Package org.djutils.data.csv
Class CsvData
java.lang.Object
org.djutils.data.csv.CsvData
CsvData takes care of reading and writing of table data in CSV format. The class can be used, e.g., as follows:
Table Table = new ListTable("data", "Table", columns); Writer writer = new FileWriter("c:/data/data.csv"); Writer metaWriter = new FileWriter("c:/data/data.meta.csv"); CsvData.writeData(writer, metaWriter, Table);
Copyright (c) 2020-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
BSD-style license. See DJUTILS License.
- Author:
- Alexander Verbraeck, Peter Knoppers, Wouter Schakel
-
Method Summary
Modifier and TypeMethodDescriptionstatic Table
Read the data from the CSV-file into the data table.static Table
Read the data from the CSV-file into the data table.static Table
Read the data from the CSV-file into the data table.static Table
readZippedData
(String fileName, String csvName, String metaName) Read the data from a CSV-file inside a zip file.static Table
readZippedData
(String fileName, String csvName, String metaName, char separator, char quotechar) Read the data from a CSV-file inside a zip file.static void
Write the data from the data table in CSV format.static void
writeData
(Writer writer, Writer metaWriter, Table table, char separator, char quotechar, de.siegmar.fastcsv.writer.LineDelimiter lineDelimiter) Write the data from the data table in CSV format.static void
Write the data from the data table in CSV format.static void
writeZippedData
(org.djutils.io.CompressedFileWriter writer, String csvName, String metaName, Table table) Write the data from the data table in CSV format.static void
writeZippedData
(org.djutils.io.CompressedFileWriter writer, String csvName, String metaName, Table table, char separator, char quotechar, de.siegmar.fastcsv.writer.LineDelimiter lineDelimiter) Write the data from the data table in CSV format.
-
Method Details
-
writeData
public static void writeData(Writer writer, Writer metaWriter, Table table, char separator, char quotechar, de.siegmar.fastcsv.writer.LineDelimiter lineDelimiter) throws IOException, TextSerializationException Write the data from the data table in CSV format. The writer writes the data, whereas the metaWriter writes the metadata. The metadata consists of a CSV file with three columns: the id, the description, and the class. The first row after the header contains the id, description, and class of the data table itself. The second and further rows contain information about the columns of the data table.- Parameters:
writer
- Writer; the writer that writes the data, e.g. to a filemetaWriter
- Writer; the writer for the metadatatable
- Table; the data table to writeseparator
- char; the delimiter to use for separating entriesquotechar
- char; the character to use for quoted elementslineDelimiter
- String; the line terminator to use, can be LineDelimiter.CR, LF, CRLF or PLATFORM- Throws:
IOException
- on I/O error when writing the dataTextSerializationException
- on unknown data type for serialization
-
writeZippedData
public static void writeZippedData(org.djutils.io.CompressedFileWriter writer, String csvName, String metaName, Table table, char separator, char quotechar, de.siegmar.fastcsv.writer.LineDelimiter lineDelimiter) throws IOException, TextSerializationException Write the data from the data table in CSV format. The data file and meta data file are zipped. The metadata consists of a CSV file with three columns: the id, the description, and the class. The first row after the header contains the id, description, and class of the data table itself. The second and further rows contain information about the columns of the data table.- Parameters:
writer
- Writer; the writer that writes the data, e.g. to a filecsvName
- String; name of the csv file within the zip filemetaName
- String; name of the meta data file within the zip filetable
- Table; the data table to writeseparator
- char; the delimiter to use for separating entriesquotechar
- char; the character to use for quoted elementslineDelimiter
- String; the line terminator to use, can be LineDelimiter.CR, LF, CRLF or PLATFORM- Throws:
IOException
- on I/O error when writing the dataTextSerializationException
- on unknown data type for serialization
-
writeZippedData
public static void writeZippedData(org.djutils.io.CompressedFileWriter writer, String csvName, String metaName, Table table) throws IOException, TextSerializationException Write the data from the data table in CSV format. The data file and meta data file are zipped. The metadata consists of a CSV file with three columns: the id, the description, and the class. The first row after the header contains the id, description, and class of the data table itself. The second and further rows contain information about the columns of the data table.- Parameters:
writer
- Writer; the writer that writes the data, e.g. to a filecsvName
- String; name of the CSV file within the zip filemetaName
- String; name of the meta data file within the zip filetable
- Table; the data table to write- Throws:
IOException
- on I/O error when writing the dataTextSerializationException
- on unknown data type for serialization
-
writeData
public static void writeData(Writer writer, Writer metaWriter, Table table) throws IOException, TextSerializationException Write the data from the data table in CSV format. The writer writes the data, whereas the metaWriter writes the metadata. The metadata consists of a CSV file with three columns: the id, the description, and the class. The first row after the header contains the id, description, and class of the data table itself. The second and further rows contain information about the columns of the data table. The line ending used will be CRLF which is RFC 4180 compliant.- Parameters:
writer
- Writer; the writer that writes the data, e.g. to a filemetaWriter
- Writer; the writer for the metadatatable
- Table; the data table to write- Throws:
IOException
- on I/O error when writing the dataTextSerializationException
- on unknown data type for serialization
-
writeData
public static void writeData(String filename, String metaFilename, Table table) throws IOException, TextSerializationException Write the data from the data table in CSV format.- Parameters:
filename
- String; the file name to write the data tometaFilename
- String; the file name to write the metadata totable
- Table; the data table to write- Throws:
IOException
- on I/O error when writing the dataTextSerializationException
- on unknown data type for serialization
-
readData
public static Table readData(Reader reader, Reader metaReader, char separator, char quotechar) throws IOException, TextSerializationException Read the data from the CSV-file into the data table. Use the metadata to reconstruct the data table.- Parameters:
reader
- Reader; the reader that can read the data, e.g. from a filemetaReader
- Reader; the writer for the metadataseparator
- char; the delimiter to use for separating entriesquotechar
- char; the character to use for quoted elements- Returns:
- Table the data table reconstructed from the meta data and filled with the data
- Throws:
IOException
- when the CSV data was not formatted rightTextSerializationException
- on unknown data type for serialization
-
readData
public static Table readData(Reader reader, Reader metaReader) throws IOException, TextSerializationException Read the data from the CSV-file into the data table. Use the metadata to reconstruct the data table.- Parameters:
reader
- Reader; the reader that can read the data, e.g. from a filemetaReader
- Reader; the writer for the metadata- Returns:
- Table the data table reconstructed from the meta data and filled with the data
- Throws:
IOException
- when the CSV data was not formatted rightTextSerializationException
- on unknown data type for serialization
-
readData
public static Table readData(String filename, String metaFilename) throws IOException, TextSerializationException Read the data from the CSV-file into the data table. Use the metadata to reconstruct the data table.- Parameters:
filename
- String; the file name to read the data frommetaFilename
- String; the file name to read the metadata from- Returns:
- Table the data table reconstructed from the meta data and filled with the data
- Throws:
IOException
- when the CSV data was not formatted rightTextSerializationException
- on unknown data type for serialization
-
readZippedData
public static Table readZippedData(String fileName, String csvName, String metaName) throws IOException, TextSerializationException Read the data from a CSV-file inside a zip file. The metadata file should be in the same zipfile. Use the metadata to reconstruct the data table.- Parameters:
fileName
- String; file name of the zip filecsvName
- String; name of the CSV-file, without pathmetaName
- String; name of the metadata file, without path- Returns:
- Table the data table reconstructed from the meta data and filled with the data
- Throws:
IOException
- when the CSV data was not formatted rightTextSerializationException
- on unknown data type for serialization
-
readZippedData
public static Table readZippedData(String fileName, String csvName, String metaName, char separator, char quotechar) throws IOException, TextSerializationException Read the data from a CSV-file inside a zip file. The metadata file should be in the same zipfile. Use the metadata to reconstruct the data table.- Parameters:
fileName
- String; file name of the zip filecsvName
- String; name of the CSV-file, without pathmetaName
- String; name of the metadata file, without pathseparator
- char; the delimiter to use for separating entriesquotechar
- char; the character to use for quoted elements- Returns:
- Table the data table reconstructed from the meta data and filled with the data
- Throws:
IOException
- when the CSV data was not formatted rightTextSerializationException
- on unknown data type for serialization
-