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