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 / SuperCSVList.java @ 47506
History | View | Annotate | Download (5.27 KB)
1 |
/*
|
---|---|
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.BufferedReader; |
9 |
import java.io.File; |
10 |
import java.io.IOException; |
11 |
import java.nio.charset.Charset; |
12 |
import java.util.Date; |
13 |
import java.util.List; |
14 |
import java.util.function.Function; |
15 |
import org.apache.commons.io.FilenameUtils; |
16 |
import org.apache.commons.io.IOUtils; |
17 |
import org.apache.commons.io.input.CloseShieldReader; |
18 |
import org.apache.commons.lang3.ArrayUtils; |
19 |
import static org.gvsig.fmap.dal.store.csv.virtualrows.RandomAccessFileReader.FILTER_NONE; |
20 |
import org.gvsig.tools.ToolsLocator; |
21 |
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer; |
22 |
import org.gvsig.tools.observer.Observable; |
23 |
import org.gvsig.tools.task.SimpleTaskStatus; |
24 |
import org.gvsig.tools.task.TaskStatus; |
25 |
import org.gvsig.tools.task.TaskStatusManager; |
26 |
import org.supercsv.io.CsvListReader; |
27 |
import org.supercsv.prefs.CsvPreference; |
28 |
|
29 |
/**
|
30 |
*
|
31 |
* @author jjdelcerro
|
32 |
*/
|
33 |
public class SuperCSVList extends AbstractCSVList { |
34 |
|
35 |
private CsvPreference preferences;
|
36 |
|
37 |
public SuperCSVList(File text, File index, Charset charset) throws IOException { |
38 |
super(text, index, charset);
|
39 |
this.preferences = CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE;
|
40 |
} |
41 |
|
42 |
public SuperCSVList(RandomAccessFileReader reader, RandomAccessFileIndex index, int skipLines) throws IOException { |
43 |
super(reader, index);
|
44 |
this.preferences = CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE;
|
45 |
this.skipLines = skipLines;
|
46 |
} |
47 |
|
48 |
public void setPreferences(CsvPreference preferences) { |
49 |
this.preferences = preferences;
|
50 |
} |
51 |
|
52 |
@Override
|
53 |
protected List<String> nextRecord() throws IOException { |
54 |
CsvListReader parser = null;
|
55 |
try {
|
56 |
CloseShieldReader theReader = CloseShieldReader.wrap(this.reader);
|
57 |
parser = new CsvListReader(theReader, this.preferences); |
58 |
List<String> values = parser.read(); |
59 |
return values;
|
60 |
} finally {
|
61 |
IOUtils.closeQuietly(parser); |
62 |
} |
63 |
} |
64 |
|
65 |
public static void main(String[] args) throws Exception { |
66 |
new DefaultLibrariesInitializer().fullInitialize();
|
67 |
|
68 |
String fname;
|
69 |
fname = "/home/fdiaz/Descargas/error_union_tablas/Expedientes_CON_REFCAT_trimmed.csv";
|
70 |
// fname = "/home/fdiaz/Descargas/origen_coordenadas.csv";
|
71 |
// fname = "/home/jjdelcerro/Descargas/test/origen_coordenadas.csv";
|
72 |
// fname = "/home/jjdelcerro/Descargas/test/esp_poblaciones.csv";
|
73 |
// fname = "/home/jjdelcerro/Descargas/test/esp_provincias.csv";
|
74 |
// fname = "/home/jjdelcerro/Descargas/test/sigpac.csv";
|
75 |
|
76 |
File data_file = new File(fname); |
77 |
File index_file = new File(FilenameUtils.removeExtension(data_file.getAbsolutePath()) + ".idx"); |
78 |
|
79 |
final TaskStatusManager taskStatusManager = ToolsLocator.getTaskStatusManager();
|
80 |
taskStatusManager.addObserver((Observable observable, Object notification) -> { |
81 |
TaskStatus status = taskStatusManager.getRunningTaskStatusMostRecent(); |
82 |
// System.out.print("\033[?25l\r");
|
83 |
// if( status!=null && status.isRunning() ) {
|
84 |
// System.out.print("\033[?25l\r");
|
85 |
// System.out.print(status.getTitle()+ " - " + status.getLabel());
|
86 |
// System.out.print("\033[K\033[?12l\033[?25h");
|
87 |
// }
|
88 |
// System.out.flush();
|
89 |
}); |
90 |
SimpleTaskStatus status = taskStatusManager.createDefaultSimpleTaskStatus(data_file.getName()); |
91 |
status.add(); |
92 |
Function<BufferedReader, Integer> numberOfLinesInRecord = (BufferedReader breader) -> { |
93 |
CloseShieldReader theReader = CloseShieldReader.wrap(breader); |
94 |
CsvListReader parser = new CsvListReader(theReader, CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE);
|
95 |
try {
|
96 |
List<String> values = parser.read(); |
97 |
} catch (IOException ex) { |
98 |
return 1; |
99 |
} |
100 |
return parser.getLineNumber();
|
101 |
}; |
102 |
RandomAccessFileReader reader = new RandomAccessFileReader(data_file, "UTF-8"); |
103 |
RandomAccessFileIndex index_lines = reader.createIndexOfLines(index_file, false, FILTER_NONE, null, numberOfLinesInRecord); |
104 |
|
105 |
CSVList csv = new SuperCSVList(reader, index_lines, 0); |
106 |
System.out.println("Lines " + csv.size()); |
107 |
int n = 0; |
108 |
for (List<String> record : csv) { |
109 |
if( n<100 ) { |
110 |
System.out.println(String.format("%6d: %s", n, ArrayUtils.toString(record))); |
111 |
} else if( n==100 ) { |
112 |
System.out.println("More records..."); |
113 |
} |
114 |
n++; |
115 |
} |
116 |
System.out.println("-----------------"); |
117 |
long t1 = new Date().getTime(); |
118 |
for (int i = csv.size() - 1; i >= 0; i--) { |
119 |
List<String> record = csv.get(i); |
120 |
if( i<100 ) { |
121 |
System.out.println(String.format("%6d: %s", i, ArrayUtils.toString(record))); |
122 |
} else if( i==100 ) { |
123 |
System.out.println("More records..."); |
124 |
} |
125 |
} |
126 |
long t2 = new Date().getTime(); |
127 |
System.out.println("Time: " + (t2 - t1) + " ms"); |
128 |
IOUtils.closeQuietly(csv); |
129 |
|
130 |
} |
131 |
|
132 |
} |