Package org.djutils.data.csv
Class CSVData
- java.lang.Object
-
- org.djutils.data.csv.CSVData
-
public final class CSVData extends Object
CSVData takes care of reading and writing of table data in CSV format. The class can be used, e.g., as follows:DataTable dataTable = new ListDataTable("data", "dataTable", columns); Writer writer = new FileWriter("c:/data/data.csv"); Writer metaWriter = new FileWriter("c:/data/data.meta.csv"); CSVData.writeData(writer, metaWriter, dataTable);
Copyright (c) 2020-2021 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See for project information https://djutils.org. The DJUTILS project is distributed under a three-clause BSD-style license, which can be found at https://djutils.org/docs/license.html.- Author:
- Alexander Verbraeck, Peter Knoppers, Wouter Schakel
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static DataTable
readData(Reader reader, Reader metaReader)
Read the data from the CSV-file into the data table.static DataTable
readData(Reader reader, Reader metaReader, char separator, char quotechar, char escapechar, String lineEnd)
Read the data from the CSV-file into the data table.static DataTable
readData(String filename, String metaFilename)
Read the data from the CSV-file into the data table.static void
writeData(Writer writer, Writer metaWriter, DataTable dataTable)
Write the data from the data table in CSV format.static void
writeData(Writer writer, Writer metaWriter, DataTable dataTable, char separator, char quotechar, char escapechar, String lineEnd)
Write the data from the data table in CSV format.static void
writeData(String filename, String metaFilename, DataTable dataTable)
Write the data from the data table in CSV format.
-
-
-
Method Detail
-
writeData
public static void writeData(Writer writer, Writer metaWriter, DataTable dataTable, char separator, char quotechar, char escapechar, String lineEnd) 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 metadatadataTable
- DataTable; the data table to writeseparator
- char; the delimiter to use for separating entriesquotechar
- char; the character to use for quoted elementsescapechar
- char; the character to use for escaping quotechars or escapecharslineEnd
- String; the line feed terminator to use- 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, DataTable dataTable) 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 metadatadataTable
- DataTable; 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, DataTable dataTable) 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 todataTable
- DataTable; the data table to write- Throws:
IOException
- on I/O error when writing the dataTextSerializationException
- on unknown data type for serialization
-
readData
public static DataTable readData(Reader reader, Reader metaReader, char separator, char quotechar, char escapechar, String lineEnd) 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 elementsescapechar
- char; the character to use for escaping quotechars or escapecharslineEnd
- String; the line feed terminator to use- Returns:
- dataTable 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 DataTable 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:
- dataTable 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 DataTable 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:
- dataTable 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
-
-