Class CSVFormat

java.lang.Object
nl.colorize.util.CSVFormat

public class CSVFormat extends Object
Describes the format of a CSV file, which is necessary since the CSV file format is not fully standardized. Instances of the format can then be used to parse CSV files into CSVRecords, or to serialize records into CSV files.

Note: This class is intended for basic CSV support. When working with CSV files produced by other applications, prefer using the FastCSV library.

  • Field Details

    • COMMA

      public static final CSVFormat COMMA
    • TAB

      public static final CSVFormat TAB
    • SEMICOLON

      public static final CSVFormat SEMICOLON
  • Method Details

    • withQuotes

      public CSVFormat withQuotes()
    • withLineSeparator

      public CSVFormat withLineSeparator(String lineSeparator)
    • of

      public CSVRecord of(List<String> columns, List<String> cells)
      Returns a CSVRecord based on this CSV format and consisting of the specified cells and column header information.
      Throws:
      IllegalArgumentException - if the number of column headers does not match the number of cells, or if there are no cells.
    • of

      public CSVRecord of(List<String> columns, String... cells)
      Returns a CSVRecord based on this CSV format and consisting of the specified cells and column header information.
      Throws:
      IllegalArgumentException - if the number of column headers does not match the number of cells, or if there are no cells.
    • of

      public CSVRecord of(List<String> cells)
      Returns a CSVRecord based on this CSV format and consisting of the specified cells. The record will not have any column header information.
      Throws:
      IllegalArgumentException - if there are no cells.
    • of

      public CSVRecord of(String... cells)
      Returns a CSVRecord based on this CSV format and consisting of the specified cells. The record will not have any column header information.
      Throws:
      IllegalArgumentException - if there are no cells.
    • parseCSV

      public List<CSVRecord> parseCSV(String csv)
      Parses the specified CSV file using this CSVFormat, and returns the resulting records. If this CSV format includes header information, the first record in the file is assumed to contain the headers.
    • toCSV

      public String toCSV(List<CSVRecord> records)
      Serializes the specified records using this CSV format. If this format includes column header information, the records will be preceded by a record containing the column headers. If the list of records is empty, this returns an empty string.
      Throws:
      IllegalStateException - if this CSV format includes column header information, but some of the records do not include column headers or include different column headers.
    • toCSV

      public String toCSV(CSVRecord record)
      Serializes the specified record using this CSV format. The serialized line will end with a trailing line separator.
    • serialize

      public <T> String serialize(List<T> rows, Function<T, CSVRecord> mapper)
      Uses this CSV format to serialize a number of objects to CSV. The specified callback function is used to map each object to a CSV record.
      Throws:
      IllegalArgumentException - if the number of column headers does not match the number of cells in the records.
    • serialize

      public <T> String serialize(List<String> columns, List<T> rows, Function<T, List<String>> mapper)
      Uses this CSV format to serialize a number of objects to CSV. The specified callback function is used to map each object to a CSV record, with each record receiving the column header information from columns.
      Throws:
      IllegalArgumentException - if the number of column headers does not match the number of cells in the records.
    • deserialize

      public <T> List<T> deserialize(List<CSVRecord> records, Function<CSVRecord, T> mapper)
      Uses this CSV format to deserialize CSV records into objects. The specified callback function is used to map each CSV record to an object.
    • deserialize

      public <T> List<T> deserialize(String csv, Function<CSVRecord, T> mapper)
      Uses this CSV format to deserialize CSV records into objects. The specified callback function is used to map each CSV record to an object.
    • withHeaders

      public static CSVFormat withHeaders(char delimiter)
      Creates a CSVFormat with the specified delimiter, which includes column header information. The first record in the CSV will be used to provide column headers to the remaining records.
    • withoutHeaders

      public static CSVFormat withoutHeaders(char delimiter)
      Creates a CSVFormat with the specified delimiter, which does not include column header information. The first record in the CSV will be parsed as a normal record, not as column headers.