1 package net.sf.pzfilereader.examples.csvperformancetest;
2
3 import java.io.BufferedReader;
4 import java.io.File;
5 import java.io.FileReader;
6 import java.util.HashMap;
7 import java.util.Map;
8
9 import net.sf.pzfilereader.DataError;
10 import net.sf.pzfilereader.DataSet;
11 import net.sf.pzfilereader.DefaultPZParserFactory;
12 import net.sf.pzfilereader.DataSet;
13 import net.sf.pzfilereader.PZParser;
14
15
16
17
18
19
20
21
22 /**
23 * @author zepernick
24 *
25 * TODO To change the template for this generated type comment go to Window -
26 * Preferences - Java - Code Style - Code Templates
27 */
28 public class CSVPerformanceTest {
29
30 public static void main(final String[] args) {
31
32 Map settings = null;
33
34 try {
35
36 settings = readSettings();
37 String filename = (String) settings.get("csvFile");
38 String verbose = (String) settings.get("verbose");
39
40 call(filename, Boolean.valueOf(verbose).booleanValue(), true);
41 } catch (final Exception ex) {
42 ex.printStackTrace();
43 }
44
45 }
46
47 public static void call(String filename, boolean verbose, boolean traverse) throws Exception, InterruptedException {
48 String[] colNames = null;
49
50
51
52 System.out.println("Parsing....");
53 final PZParser pzparser = DefaultPZParserFactory.getInstance().newDelimitedParser(new File(filename),
54 ',', '"');
55 long timeStarted = System.currentTimeMillis();
56 final DataSet ds = pzparser.parse();
57 long timeFinished = System.currentTimeMillis();
58
59 String timeMessage = "";
60
61 if (timeFinished - timeStarted < 1000) {
62 timeMessage = (timeFinished - timeStarted) + " Milleseconds...";
63 } else {
64 timeMessage = ((float) ((timeFinished - timeStarted) / 1000.0)) + " Seconds...";
65 }
66
67 System.out.println("");
68 System.out.println("********FILE PARSED IN: " + timeMessage + " ******");
69
70 if (traverse) {
71 if (verbose) {
72 Thread.sleep(2000);
73
74 }
75 timeStarted = System.currentTimeMillis();
76 colNames = ds.getColumns();
77 int rowCount = 0;
78 int colCount = colNames.length;
79 while (ds.next()) {
80 rowCount++;
81 for (int i = 0; i < colNames.length; i++) {
82 String string = ds.getString(colNames[i]);
83
84 if (verbose) {
85 System.out.println("COLUMN NAME: " + colNames[i] + " VALUE: " + string);
86 }
87 }
88
89 if (verbose) {
90 System.out.println("===========================================================================");
91 }
92 }
93 timeFinished = System.currentTimeMillis();
94
95 if (timeFinished - timeStarted < 1000) {
96 timeMessage = (timeFinished - timeStarted) + " Milleseconds...";
97 } else {
98 timeMessage = ((float)((timeFinished - timeStarted) / 1000.0)) + " Seconds...";
99 }
100
101 System.out.println("");
102 System.out.println("********Traversed Data In: " + timeMessage + " (rows: " + rowCount + " Col:" + colCount
103 + ") ******");
104
105 }
106
107 if (ds.getErrors() != null && ds.getErrors().size() > 0) {
108 System.out.println("FOUND ERRORS IN FILE....");
109 for (int i = 0; i < ds.getErrors().size(); i++) {
110 final DataError de = (DataError) ds.getErrors().get(i);
111 System.out.println("Error: " + de.getErrorDesc() + " Line: " + de.getLineNo());
112 }
113 }
114
115 }
116
117 private static Map readSettings() throws Exception {
118 final Map result = new HashMap();
119 FileReader fr = null;
120 BufferedReader br = null;
121 String line = null;
122
123 try {
124 fr = new FileReader("settings.properties");
125 br = new BufferedReader(fr);
126
127 while ((line = br.readLine()) != null) {
128 if (line.trim().length() == 0 || line.startsWith("#") || line.indexOf("=") == -1) {
129 continue;
130 }
131
132 result.put(line.substring(0, line.indexOf("=")), line.substring(line.indexOf("=") + 1));
133 }
134 } finally {
135 if (fr != null) {
136 fr.close();
137 }
138 if (br != null) {
139 br.close();
140 }
141 }
142
143 return result;
144
145 }
146
147 }