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