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-2021 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       * @throws IOException on I/O error when writing the data
50       * @throws TextSerializationException on unknown data type for serialization
51       */
52      public static void writeData(final Writer writer, final Writer metaWriter, final DataTable dataTable)
53              throws IOException, TextSerializationException
54      {
55          CSVData.writeData(writer, metaWriter, dataTable, '\t', '\u0000', '\\', "\n");
56      }
57  
58      /**
59       * Write the data from the data table in TSV format.
60       * @param filename String; the file name to write the data to
61       * @param metaFilename String; the file name to write the metadata to
62       * @param dataTable DataTable; the data table to write
63       * @throws IOException on I/O error when writing the data
64       * @throws TextSerializationException on unknown data type for serialization
65       */
66      public static void writeData(final String filename, final String metaFilename, final DataTable dataTable)
67              throws IOException, TextSerializationException
68      {
69          FileWriter fw = null;
70          FileWriter mfw = null;
71          try
72          {
73              fw = new FileWriter(filename);
74              mfw = new FileWriter(metaFilename);
75              writeData(fw, mfw, dataTable);
76          }
77          finally
78          {
79              if (null != fw)
80              {
81                  fw.close();
82              }
83              if (null != mfw)
84              {
85                  mfw.close();
86              }
87          }
88      }
89  
90      /**
91       * Read the data from the TSV-file into the data table. Use the metadata to reconstruct the data table.
92       * @param reader Reader; the reader that can read the data, e.g. from a file
93       * @param metaReader Reader; the writer for the metadata
94       * @return dataTable the data table reconstructed from the meta data and filled with the data
95       * @throws IOException on I/O error when reading the data
96       * @throws TextSerializationException on unknown data type for serialization
97       */
98      public static DataTable readData(final Reader reader, final Reader metaReader)
99              throws IOException, TextSerializationException
100     {
101         return CSVData.readData(reader, metaReader, '\t', '\u0000', '\\', "\n");
102     }
103 
104     /**
105      * Read the data from the TSV-file into the data table. Use the metadata to reconstruct the data table.
106      * @param filename String; the file name to read the data from
107      * @param metaFilename String; the file name to read the metadata from
108      * @return dataTable the data table reconstructed from the meta data and filled with the data
109      * @throws IOException on I/O error when reading the data
110      * @throws TextSerializationException on unknown data type for serialization
111      */
112     public static DataTable readData(final String filename, final String metaFilename)
113             throws IOException, TextSerializationException
114     {
115         FileReader fr = null;
116         FileReader mfr = null;
117         try
118         {
119             fr = new FileReader(filename);
120             mfr = new FileReader(metaFilename);
121             return readData(fr, mfr);
122         }
123         finally
124         {
125             if (null != fr)
126             {
127                 fr.close();
128             }
129             if (null != mfr)
130             {
131                 mfr.close();
132             }
133         }
134     }
135 
136 }