JavaXT
|
|
CSV ClassProvides static methods used to parse tabular data stored in plain text where records (aka rows) are separated with a line break and columns are delimited with a character (comma, tab, pipe, etc). CSV files is an example of such tabular data which uses commas to separate values in a row. Here's an example of how to parse a CSV file using the static methods found in this class:
javaxt.io.File csvFile = new javaxt.io.File("/temp/employees.csv"); try (java.io.BufferedReader is = csvFile.getBufferedReader("UTF-8")){ //Read header String header = CSV.readLine(is); //Remove the Byte Order Mark (BOM) if there is one int bom = CSV.getByteOrderMark(header); if (bom>-1) header = header.substring(bom); //Parse header ArrayList<String> headers = new ArrayList<>(); for (javaxt.utils.Value col : CSV.getColumns(header, ",")){ headers.add(col.toString()); } //Read rows String row; while (!(row=CSV.readLine(is)).isEmpty()){ //Parse row CSV.Columns columns = CSV.getColumns(row, ","); for (int i=0; i<columns.length(); i++){ String colName = headers.get(i); String colValue = columns.get(i).toString(); System.out.println(colName + ": " + colValue); } System.out.println("---------------------------"); } } ConstructorsThere are no public constructors.PropertiesTAB_DELIMITER
COMMA_DELIMITER
Static MethodsgetColumns( String row, String delimiter ) returns Columns Returns column values for a given row readLine( String data ) returns String Returns a substring for the given data, ending at the first line break that is not inside a quote readLine( java.io.InputStream is ) returns String Returns a row of data from an InputStream. This method will read characters one at a time until it reaches a line break that is not inside a double quote. Depending on the source of the InputStream, this method may be significantly slower than the other readLine() method that uses a BufferedReader. Example usage:
//Create an input stream java.io.InputStream is = ... //Read header String header = CSV.readLine(is); int bom = CSV.getByteOrderMark(header); if (bom>-1) header = header.substring(bom); console.log(header); //Read rows String row; while (!(row=CSV.readLine(is)).isEmpty()){ console.log(row); } readLine( java.io.BufferedReader reader ) returns String Returns a row of data from a BufferedReader. Unlike the BufferedReader readLine() method, this method will not stop at line breaks inside a double quote. Note that a BufferedReader is significantly faster than an InputStream when reading files. Example usage:
//Open input stream from an javaxt.io.File try (java.io.BufferedReader is = file.getBufferedReader("UTF-8")){ //Read header String header = CSV.readLine(is); int bom = CSV.getByteOrderMark(header); if (bom>-1) header = header.substring(bom); console.log(header); //Read rows String row; while (!(row=CSV.readLine(is)).isEmpty()){ console.log(row); } } getByteOrderMark( String str ) returns int Returns end position of the Byte Order Mark (BOM). Example usage:
int bom = CSV.getByteOrderMark(header); if (bom>-1) header = header.substring(bom); startsWithByteOrderMark( String str ) returns boolean Returns true if the given string starts with a Byte Order Mark (BOM) Public Classes |