svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.file / org.gvsig.fmap.dal.file.csv / src / main / java / org / gvsig / fmap / dal / store / csv / virtualrows / ApacheCommonsCSVList.java @ 46338
History | View | Annotate | Download (4.26 KB)
1 | 45727 | jjdelcerro | /*
|
---|---|---|---|
2 | * To change this license header, choose License Headers in Project Properties.
|
||
3 | * To change this template file, choose Tools | Templates
|
||
4 | * and open the template in the editor.
|
||
5 | */
|
||
6 | package org.gvsig.fmap.dal.store.csv.virtualrows; |
||
7 | |||
8 | import java.io.Closeable; |
||
9 | import java.io.File; |
||
10 | import java.io.IOException; |
||
11 | import java.nio.charset.Charset; |
||
12 | import java.util.ArrayList; |
||
13 | import java.util.Date; |
||
14 | import java.util.Iterator; |
||
15 | import java.util.List; |
||
16 | import org.apache.commons.csv.CSVFormat; |
||
17 | import org.apache.commons.csv.CSVParser; |
||
18 | import org.apache.commons.csv.CSVRecord; |
||
19 | import org.apache.commons.io.FilenameUtils; |
||
20 | import org.apache.commons.io.IOUtils; |
||
21 | import org.apache.commons.lang3.ArrayUtils; |
||
22 | import static org.gvsig.fmap.dal.store.csv.virtualrows.RandomAccessFileReader.FILTER_NONE; |
||
23 | import org.gvsig.tools.ToolsLocator; |
||
24 | import org.gvsig.tools.library.impl.DefaultLibrariesInitializer; |
||
25 | import org.gvsig.tools.observer.Observable; |
||
26 | import org.gvsig.tools.task.SimpleTaskStatus; |
||
27 | import org.gvsig.tools.task.TaskStatus; |
||
28 | import org.gvsig.tools.task.TaskStatusManager; |
||
29 | |||
30 | /**
|
||
31 | *
|
||
32 | * @author gvSIG Team
|
||
33 | */
|
||
34 | public class ApacheCommonsCSVList extends AbstractCSVList { |
||
35 | |||
36 | public ApacheCommonsCSVList(File text, File index, Charset charset) throws IOException { |
||
37 | super(text, index, charset);
|
||
38 | } |
||
39 | |||
40 | public ApacheCommonsCSVList(RandomAccessFileReader reader, RandomAccessFileIndex index) throws IOException { |
||
41 | super(reader, index);
|
||
42 | } |
||
43 | |||
44 | @Override
|
||
45 | protected List<String> nextRecord() throws IOException { |
||
46 | CSVParser parser = CSVFormat.EXCEL.parse(this.reader);
|
||
47 | Iterator<CSVRecord> it = parser.iterator();
|
||
48 | if (!it.hasNext()) {
|
||
49 | return null; |
||
50 | } |
||
51 | CSVRecord record = it.next(); |
||
52 | List<String> values = new ArrayList<>(); |
||
53 | for (String value : record) { |
||
54 | values.add(value); |
||
55 | } |
||
56 | return values;
|
||
57 | } |
||
58 | |||
59 | public static void main(String[] args) throws Exception { |
||
60 | new DefaultLibrariesInitializer().fullInitialize();
|
||
61 | |||
62 | String fname;
|
||
63 | fname = "/home/jjdelcerro/Descargas/test/origen_coordenadas.csv";
|
||
64 | // fname = "/home/jjdelcerro/Descargas/test/esp_poblaciones.csv";
|
||
65 | // fname = "/home/jjdelcerro/Descargas/test/esp_provincias.csv";
|
||
66 | |||
67 | File data_file = new File(fname); |
||
68 | File index_file = new File(FilenameUtils.removeExtension(data_file.getAbsolutePath()) + ".idx"); |
||
69 | |||
70 | final TaskStatusManager taskStatusManager = ToolsLocator.getTaskStatusManager();
|
||
71 | taskStatusManager.addObserver((Observable observable, Object notification) -> { |
||
72 | TaskStatus status = taskStatusManager.getRunningTaskStatusMostRecent(); |
||
73 | // System.out.print("\033[?25l\r");
|
||
74 | // if( status!=null && status.isRunning() ) {
|
||
75 | // System.out.print("\033[?25l\r");
|
||
76 | // System.out.print(status.getTitle()+ " - " + status.getLabel());
|
||
77 | // System.out.print("\033[K\033[?12l\033[?25h");
|
||
78 | // }
|
||
79 | // System.out.flush();
|
||
80 | }); |
||
81 | SimpleTaskStatus status = taskStatusManager.createDefaultSimpleTaskStatus(data_file.getName()); |
||
82 | status.add(); |
||
83 | |||
84 | RandomAccessFileReader reader = new RandomAccessFileReader(data_file, "UTF-8"); |
||
85 | RandomAccessFileIndex index_lines = reader.createOrOpenIndexOfLines(index_file, FILTER_NONE, null);
|
||
86 | |||
87 | CSVList csv = new ApacheCommonsCSVList(reader, index_lines);
|
||
88 | System.out.println("Lines " + csv.size()); |
||
89 | int n = 0; |
||
90 | for (List<String> record : csv) { |
||
91 | if( n<100 ) { |
||
92 | System.out.println(String.format("%6d: %s", n, ArrayUtils.toString(record))); |
||
93 | } else if( n==100 ) { |
||
94 | System.out.println("More records..."); |
||
95 | } |
||
96 | n++; |
||
97 | } |
||
98 | System.out.println("-----------------"); |
||
99 | long t1 = new Date().getTime(); |
||
100 | for (int i = csv.size() - 1; i >= 0; i--) { |
||
101 | List<String> record = csv.get(i); |
||
102 | if( i<100 ) { |
||
103 | System.out.println(String.format("%6d: %s", i, ArrayUtils.toString(record))); |
||
104 | } else if( i==100 ) { |
||
105 | System.out.println("More records..."); |
||
106 | } |
||
107 | } |
||
108 | long t2 = new Date().getTime(); |
||
109 | System.out.println("Time: " + (t2 - t1) + " ms"); |
||
110 | IOUtils.closeQuietly(csv); |
||
111 | |||
112 | } |
||
113 | |||
114 | } |