Class CSV

  • All Implemented Interfaces:
    Serializable

    public class CSV
    extends Object
    implements Serializable

    Specification of a delimited separated file, or more commonly refereed to as CSV. Provides methods to build a CSV specification and then create a reader, writer, or formatter.

    Author:
    David B. Bracewell
    See Also:
    Serialized Form
    • Constructor Detail

      • CSV

        public CSV()
    • Method Detail

      • builder

        public static CSV builder()

        Creates a CSV builder object

        Returns:
        the csv builder builder object
      • csv

        public static CSV csv()

        Convenience method for quickly retrieving a csv version of a CSV builder

        Returns:
        the csv builder builder with delimiter set to a comma character
      • tsv

        public static CSV tsv()

        Convenience method for quickly retrieving a tsv version of a CSV builder

        Returns:
        the csv builder builder with delimiter set to a tab character
      • comment

        public CSV comment​(char commentChar)
        Sets the character that signifies a comment
        Parameters:
        commentChar - the comment char
        Returns:
        the csv builder
      • delimiter

        public CSV delimiter​(char delimiter)
        Sets the delimiter character
        Parameters:
        delimiter - the delimiter
        Returns:
        the csv builder
      • escape

        public CSV escape​(char escape)
        Sets the escape character
        Parameters:
        escape - the escape
        Returns:
        the csv builder
      • formatter

        public CSVFormatter formatter()
        Creates a CSVFormatter using this specification
        Returns:
        the CSVFormatter
      • getComment

        public char getComment()
        Gets the comment character.
        Returns:
        the comment character.
      • getDelimiter

        public char getDelimiter()
        Gets the delimiter character.
        Returns:
        the delimiter character
      • getEscape

        public char getEscape()
        Gets the escape character.
        Returns:
        the escape character
      • getHasHeader

        public boolean getHasHeader()
        Determines if the CSV file is expected to have a header or not
        Returns:
        True if the first line of the csv file is the header, false if there is no header
      • getHeader

        public List<String> getHeader()
        Gets the name of the column headers.
        Returns:
        the names of the column headers
      • getQuote

        public char getQuote()
        Gets the quote character.
        Returns:
        the quote character
      • hasHeader

        public CSV hasHeader()
        Specifies that the CSV file has a header.
        Returns:
        the csv builder builder
      • hasHeader

        public CSV hasHeader​(boolean hasHeader)
        Specifies whether or not the CSV file has a header.
        Parameters:
        hasHeader - true the csv has a header, false it does not
        Returns:
        the csv builder
      • header

        public CSV header​(String... items)
        Specifies the names of the items in the CSV header.
        Parameters:
        items - the names of the columns (i.e. header names)
        Returns:
        the csv builder
      • header

        public CSV header​(List<String> items)
        Specifies the names of the items in the CSV header.
        Parameters:
        items - the names of the columns (i.e. header names)
        Returns:
        the csv builder
      • isKeepEmptyCells

        public boolean isKeepEmptyCells()
        Determines if empty cells should be kept or not
        Returns:
        True if empty cells are kept, False if they are removed
      • iterator

        public Iterator<List<String>> iterator​(@NonNull
                                               @NonNull Resource resource)
                                        throws IOException
        Creates an iterator over the rows of the csv file
        Parameters:
        resource - the resource to read from
        Returns:
        the stream of items in the csv file
        Throws:
        IOException - Something went wrong reading the file
      • keepEmptyCells

        public CSV keepEmptyCells()
        Specifies that empty cells should be kept
        Returns:
        the csv builder
      • quote

        public CSV quote​(char quote)
        Sets the quote character
        Parameters:
        quote - the quote
        Returns:
        the csv builder
      • reader

        public CSVReader reader​(Reader reader)
                         throws IOException
        Creates a CSVReader using this specification from a given reader
        Parameters:
        reader - the reader to wrap
        Returns:
        The CSVReader
        Throws:
        IOException - Something went wrong initializing the reader
      • reader

        public CSVReader reader​(Resource resource)
                         throws IOException
        Creates a CSVReader using this specification from a given resource
        Parameters:
        resource - the resource to read from
        Returns:
        The CSVReader
        Throws:
        IOException - Something went wrong initializing the resource
      • removeEmptyCells

        public CSV removeEmptyCells()
        Specifies that empty cells should be removed
        Returns:
        the csv builder
      • rowListStream

        public Stream<List<String>> rowListStream​(@NonNull
                                                  @NonNull Resource resource)
                                           throws IOException
        Creates a reusable Java Stream over the rows of the csv file
        Parameters:
        resource - the resource to read from
        Returns:
        the stream of items in the csv file
        Throws:
        IOException - Something went wrong reading the file
      • rowMapStream

        public Stream<Map<String,​String>> rowMapStream​(@NonNull
                                                             @NonNull Resource resource)
                                                      throws IOException
        Creates a reusable Java Stream over the rows of the csv file returning them as map with key as the column name and value as the value. When the column is not in the header the column name will be "AutoColumn-" and column number.
        Parameters:
        resource - the resource to read from
        Returns:
        the stream of items in the csv file
        Throws:
        IOException - Something went wrong reading the file
      • writer

        public CSVWriter writer​(Writer writer)
                         throws IOException
        Creates a CSVWriter using this specification from a given writer
        Parameters:
        writer - the writer to wrap
        Returns:
        The CSVWriter
        Throws:
        IOException - Something went wrong initializing the writer
      • writer

        public CSVWriter writer​(Resource resource)
                         throws IOException
        Creates a CSVWriter using this specification from a given resource
        Parameters:
        resource - the resource to write to
        Returns:
        The CSVWriter
        Throws:
        IOException - Something went wrong initializing the resource