5
5
//** CSV
6
6
//******************************************************************************
7
7
/**
8
- * Provides static methods used to parse comma and tab delimited files
8
+ * Provides static methods used to parse tabular data stored in plain text
9
+ * where records (aka rows) are separated with a line break and columns are
10
+ * delimited with a character (comma, tab, pipe, etc). CSV files is an
11
+ * example of such tabular data which uses commas to separate values in a
12
+ * row. <p/>
13
+ *
14
+ * Here's an example of how to parse a CSV file using the static methods
15
+ * found in this class:
16
+ <pre>
17
+ javaxt.io.File csvFile = new javaxt.io.File("/temp/employees.csv");
18
+ try (java.io.BufferedReader is = csvFile.getBufferedReader("UTF-8")){
19
+
20
+
21
+ //Read header
22
+ String header = CSV.readLine(is);
23
+
24
+ //Remove the Byte Order Mark (BOM) if there is one
25
+ int bom = CSV.getByteOrderMark(header);
26
+ if (bom>-1) header = header.substring(bom);
27
+
28
+
29
+ //Parse header
30
+ ArrayList<String> headers = new ArrayList<>();
31
+ for (javaxt.utils.Value col : CSV.getColumns(header, ",")){
32
+ headers.add(col.toString());
33
+ }
34
+
35
+
36
+ //Read rows
37
+ String row;
38
+ while (!(row=CSV.readLine(is)).isEmpty()){
39
+
40
+
41
+ //Parse row
42
+ CSV.Columns columns = CSV.getColumns(row, ",");
43
+ for (int i=0; i<columns.length(); i++){
44
+ String colName = headers.get(i);
45
+ String colValue = columns.get(i).toString();
46
+ System.out.println(colName + ": " + colValue);
47
+ }
48
+
49
+ System.out.println("---------------------------");
50
+ }
51
+
52
+ }
53
+ </pre>
54
+ *
9
55
*
10
56
******************************************************************************/
11
57
@@ -15,12 +61,13 @@ public class CSV {
15
61
public static final String COMMA_DELIMITER = "," ;
16
62
//public static final String UTF8_BOM = "\uFEFF";
17
63
64
+
18
65
//**************************************************************************
19
66
//** Columns
20
67
//**************************************************************************
21
- /** Used to represent column values
68
+ /** Class used to encapsulate columns in a row
22
69
*/
23
- public static class Columns {
70
+ public static class Columns implements Iterable < javaxt . utils . Value > {
24
71
private ArrayList <javaxt .utils .Value > cols ;
25
72
public Columns (){
26
73
cols = new ArrayList <>();
@@ -39,6 +86,11 @@ public javaxt.utils.Value get(int idx){
39
86
public int length (){
40
87
return cols .size ();
41
88
}
89
+
90
+ @ Override
91
+ public java .util .Iterator <javaxt .utils .Value > iterator () {
92
+ return cols .iterator ();
93
+ }
42
94
}
43
95
44
96
@@ -168,7 +220,7 @@ public static String readLine(java.io.InputStream is) throws java.io.IOException
168
220
<pre>
169
221
170
222
//Open input stream from an javaxt.io.File
171
- try (java.io.BufferedReader is = file.getBufferedReader("UTF-8)){
223
+ try (java.io.BufferedReader is = file.getBufferedReader("UTF-8" )){
172
224
173
225
//Read header
174
226
String header = CSV.readLine(is);
0 commit comments