1 package org.djutils.data.csv;
2
3 import java.io.FileReader;
4 import java.io.FileWriter;
5 import java.io.IOException;
6 import java.io.Reader;
7 import java.io.Writer;
8
9 import org.djutils.data.DataTable;
10 import org.djutils.data.serialization.TextSerializationException;
11
12 import com.opencsv.ICSVWriter;
13 import com.opencsv.exceptions.CsvValidationException;
14
15 /**
16 * TSVData takes care of reading and writing of table data in Tab-Separated-Value format. The class can be used, e.g., as
17 * follows:
18 *
19 * <pre>
20 * DataTable dataTable = new ListDataTable("data", "dataTable", columns);
21 * Writer writer = new FileWriter("c:/data/data.tsv");
22 * Writer metaWriter = new FileWriter("c:/data/data.meta.tsv");
23 * TSVData.writeData(writer, metaWriter, dataTable);
24 * </pre>
25 *
26 * Copyright (c) 2020-2020 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
27 * for project information <a href="https://djutils.org" target="_blank"> https://djutils.org</a>. The DJUTILS project is
28 * distributed under a three-clause BSD-style license, which can be found at
29 * <a href="https://djutils.org/docs/license.html" target="_blank"> https://djutils.org/docs/license.html</a>. <br>
30 * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
31 * @author <a href="https://www.tudelft.nl/pknoppers">Peter Knoppers</a>
32 * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
33 */
34 public final class TSVData
35 {
36 /**
37 * Utility class, no public constructor.
38 */
39 private TSVData()
40 {
41 // utility class
42 }
43
44 /**
45 * Write the data from the data table in TSV format. The writer writes the data, whereas the metaWriter writes the metadata.
46 * The metadata consists of a TSV file with three columns: the id, the description, and the class. The first row after the
47 * header contains the id, description, and class of the data table itself. The second and further rows contain information
48 * about the columns of the data table.
49 * @param writer Writer; the writer that writes the data, e.g. to a file
50 * @param metaWriter Writer; the writer for the metadata
51 * @param dataTable the data table to write
52 * @throws IOException on I/O error when writing the data
53 * @throws TextSerializationException on unknown data type for serialization
54 */
55 public static void writeData(final Writer writer, final Writer metaWriter, final DataTable dataTable)
56 throws IOException, TextSerializationException
57 {
58 CSVData.writeData(writer, metaWriter, dataTable, '\t', '\u0000', '\\', ICSVWriter.DEFAULT_LINE_END);
59 }
60
61 /**
62 * Write the data from the data table in TSV format.
63 * @param filename String; the file name to write the data to
64 * @param metaFilename String; the file name to write the metadata to
65 * @param dataTable the data table to write
66 * @throws IOException on I/O error when writing the data
67 * @throws TextSerializationException on unknown data type for serialization
68 */
69 public static void writeData(final String filename, final String metaFilename, final DataTable dataTable)
70 throws IOException, TextSerializationException
71 {
72 FileWriter fw = null;
73 FileWriter mfw = null;
74 try
75 {
76 fw = new FileWriter(filename);
77 mfw = new FileWriter(metaFilename);
78 writeData(fw, mfw, dataTable);
79 }
80 finally
81 {
82 if (null != fw)
83 {
84 fw.close();
85 }
86 if (null != mfw)
87 {
88 mfw.close();
89 }
90 }
91 }
92
93 /**
94 * Read the data from the TSV-file into the data table. Use the metadata to reconstruct the data table.
95 * @param reader Reader; the reader that can read the data, e.g. from a file
96 * @param metaReader Reader; the writer for the metadata
97 * @return dataTable the data table reconstructed from the meta data and filled with the data
98 * @throws IOException on I/O error when reading the data
99 * @throws CsvValidationException when the TSV data was not formatted right
100 * @throws TextSerializationException on unknown data type for serialization
101 */
102 public static DataTable readData(final Reader reader, final Reader metaReader)
103 throws IOException, CsvValidationException, TextSerializationException
104 {
105 return CSVData.readData(reader, metaReader, '\t', '\u0000', '\\', ICSVWriter.DEFAULT_LINE_END);
106 }
107
108 /**
109 * Read the data from the TSV-file into the data table. Use the metadata to reconstruct the data table.
110 * @param filename String; the file name to read the data from
111 * @param metaFilename String; the file name to read the metadata from
112 * @return dataTable the data table reconstructed from the meta data and filled with the data
113 * @throws IOException on I/O error when reading the data
114 * @throws CsvValidationException when the TSV data was not formatted right
115 * @throws TextSerializationException on unknown data type for serialization
116 */
117 public static DataTable readData(final String filename, final String metaFilename)
118 throws IOException, CsvValidationException, TextSerializationException
119 {
120 FileReader fr = null;
121 FileReader mfr = null;
122 try
123 {
124 fr = new FileReader(filename);
125 mfr = new FileReader(metaFilename);
126 return readData(fr, mfr);
127 }
128 finally
129 {
130 if (null != fr)
131 {
132 fr.close();
133 }
134 if (null != mfr)
135 {
136 mfr.close();
137 }
138 }
139 }
140
141 }