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  /**
13   * TSVData takes care of reading and writing of table data in Tab-Separated-Value format. The class can be used, e.g., as
14   * follows:
15   * 
16   * <pre>
17   * DataTable dataTable = new ListDataTable("data", "dataTable", columns);
18   * Writer writer = new FileWriter("c:/data/data.tsv");
19   * Writer metaWriter = new FileWriter("c:/data/data.meta.tsv");
20   * TSVData.writeData(writer, metaWriter, dataTable);
21   * </pre>
22   * 
23   * Copyright (c) 2020-2020 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
24   * for project information <a href="https://djutils.org" target="_blank"> https://djutils.org</a>. The DJUTILS project is
25   * distributed under a three-clause BSD-style license, which can be found at
26   * <a href="https://djutils.org/docs/license.html" target="_blank"> https://djutils.org/docs/license.html</a>. <br>
27   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
28   * @author <a href="https://www.tudelft.nl/pknoppers">Peter Knoppers</a>
29   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
30   */
31  public final class TSVData
32  {
33      /**
34       * Utility class, no public constructor.
35       */
36      private TSVData()
37      {
38          // utility class
39      }
40  
41      /**
42       * Write the data from the data table in TSV format. The writer writes the data, whereas the metaWriter writes the metadata.
43       * The metadata consists of a TSV file with three columns: the id, the description, and the class. The first row after the
44       * header contains the id, description, and class of the data table itself. The second and further rows contain information
45       * about the columns of the data table.
46       * @param writer Writer; the writer that writes the data, e.g. to a file
47       * @param metaWriter Writer; the writer for the metadata
48   * @param dataTable DataTable; the data table to write
49  
50       * @throws IOException on I/O error when writing the data
51       * @throws TextSerializationException on unknown data type for serialization
52       */
53      public static void writeData(final Writer writer, final Writer metaWriter, final DataTable dataTable)
54              throws IOException, TextSerializationException
55      {
56          CSVData.writeData(writer, metaWriter, dataTable, '\t', '\u0000', '\\', "\n");
57      }
58  
59      /**
60       * Write the data from the data table in TSV format.
61       * @param filename String; the file name to write the data to
62       * @param metaFilename String; the file name to write the metadata to
63   * @param dataTable DataTable; the data table to write
64  
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', '\\', "\n");
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 }