Skip to content

Commit dacd2bb

Browse files
committed
Updated the Columns to be Iterable and updated java docs in the CSV class
1 parent 1f741cf commit dacd2bb

File tree

1 file changed

+56
-4
lines changed

1 file changed

+56
-4
lines changed

src/javaxt/express/utils/CSV.java

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,53 @@
55
//** CSV
66
//******************************************************************************
77
/**
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&lt;String> headers = new ArrayList&lt;>();
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&lt;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+
*
955
*
1056
******************************************************************************/
1157

@@ -15,12 +61,13 @@ public class CSV {
1561
public static final String COMMA_DELIMITER = ",";
1662
//public static final String UTF8_BOM = "\uFEFF";
1763

64+
1865
//**************************************************************************
1966
//** Columns
2067
//**************************************************************************
21-
/** Used to represent column values
68+
/** Class used to encapsulate columns in a row
2269
*/
23-
public static class Columns {
70+
public static class Columns implements Iterable<javaxt.utils.Value> {
2471
private ArrayList<javaxt.utils.Value> cols;
2572
public Columns(){
2673
cols = new ArrayList<>();
@@ -39,6 +86,11 @@ public javaxt.utils.Value get(int idx){
3986
public int length(){
4087
return cols.size();
4188
}
89+
90+
@Override
91+
public java.util.Iterator<javaxt.utils.Value> iterator() {
92+
return cols.iterator();
93+
}
4294
}
4395

4496

@@ -168,7 +220,7 @@ public static String readLine(java.io.InputStream is) throws java.io.IOException
168220
<pre>
169221
170222
//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")){
172224
173225
//Read header
174226
String header = CSV.readLine(is);

0 commit comments

Comments
 (0)