Package jslx

Class CSVUtil


  • public class CSVUtil
    extends java.lang.Object
    A class to facilitate some basic CSV processing without having to worry about underlying csv library. Helps with reading and writing arrays to csv files. Generally, exceptions are squashed.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static org.slf4j.Logger LOGGER  
    • Constructor Summary

      Constructors 
      Constructor Description
      CSVUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.util.Iterator<java.lang.String[]> getCSVIterator​(java.nio.file.Path pathToFile)
      Returns an iterator to the rows of a csv file that may have the first row as a header of column labels and each subsequent row as the data for each column, e.g.
      static java.util.List<java.lang.String[]> readRows​(int skipLines, java.nio.file.Path pathToFile)
      Reads all rows from a csv file that may have the first row as a header of column labels and each subsequent row as the data for each column, e.g.
      static java.util.List<java.lang.String[]> readRows​(java.nio.file.Path pathToFile)
      Reads all rows from a csv file that may have the first row as a header of column labels and each subsequent row as the data for each column, e.g.
      static double[][] readToColumns​(java.util.List<java.lang.String> names, java.nio.file.Path pathToFile)
      Reads data from a csv file that has the first row as a header of column labels and each subsequent row as the data for each column, e.g.
      static double[][] readToRows​(java.util.List<java.lang.String> names, java.nio.file.Path pathToFile)
      Reads data from a csv file that has the first row as a header of column labels and each subsequent row as the data for each row, e.g.
      static void writeArrayToCSVFile​(java.util.List<java.lang.String> header, double[][] array, java.nio.file.Path pathToFile)
      IOException is squelched with a warning to the logger if there was a problem writing to the file.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • LOGGER

        public static final org.slf4j.Logger LOGGER
    • Constructor Detail

      • CSVUtil

        public CSVUtil()
    • Method Detail

      • readRows

        public static java.util.List<java.lang.String[]> readRows​(java.nio.file.Path pathToFile)
        Reads all rows from a csv file that may have the first row as a header of column labels and each subsequent row as the data for each column, e.g. "x", "y" 1.1, 2.0 4.3, 6.4 etc. This method squelches any IOExceptions
        Parameters:
        pathToFile - the path to the file
        Returns:
        the filled list
      • readRows

        public static java.util.List<java.lang.String[]> readRows​(int skipLines,
                                                                  java.nio.file.Path pathToFile)
        Reads all rows from a csv file that may have the first row as a header of column labels and each subsequent row as the data for each column, e.g. "x", "y" 1.1, 2.0 4.3, 6.4 etc. This method squelches any IOExceptions. Writes warning to log. If there was a problem an empty list is returned.
        Parameters:
        skipLines - the number of lines to skip from the top of the file
        pathToFile - the path to the file
        Returns:
        the filled list
      • getCSVIterator

        public static java.util.Iterator<java.lang.String[]> getCSVIterator​(java.nio.file.Path pathToFile)
        Returns an iterator to the rows of a csv file that may have the first row as a header of column labels and each subsequent row as the data for each column, e.g. "x", "y" 1.1, 2.0 4.3, 6.4 etc. This method squelches any IOExceptions. An iterator with no elements is returned if there is a problem.
        Parameters:
        pathToFile - the path to the file
        Returns:
        the filled list
      • readToRows

        public static double[][] readToRows​(java.util.List<java.lang.String> names,
                                            java.nio.file.Path pathToFile)
        Reads data from a csv file that has the first row as a header of column labels and each subsequent row as the data for each row, e.g. "x", "y" 1.1, 2.0 4.3, 6.4 etc. The List names will hold ("x", "y"). If names has strings it will be cleared. The returned array will hold data[0] = {1.1, 2.0} data[1] = {4.3, 6.4} etc.
        Parameters:
        names - the list to fill with header names
        pathToFile - the path to the file
        Returns:
        the filled array of arrays
      • readToColumns

        public static double[][] readToColumns​(java.util.List<java.lang.String> names,
                                               java.nio.file.Path pathToFile)
        Reads data from a csv file that has the first row as a header of column labels and each subsequent row as the data for each column, e.g. "x", "y" 1.1, 2.0 4.3, 6.4 etc. The List names will hold ("x", "y"). If names has strings it will be cleared. The returned array will hold data[0] = {1.1, 4.3} data[1] = {2.0, 6.4} etc.
        Parameters:
        names - the list to fill with header names
        pathToFile - the path to the file
        Returns:
        the filled array of arrays
      • writeArrayToCSVFile

        public static void writeArrayToCSVFile​(java.util.List<java.lang.String> header,
                                               double[][] array,
                                               java.nio.file.Path pathToFile)
        IOException is squelched with a warning to the logger if there was a problem writing to the file.
        Parameters:
        header - the names of the columns as strings
        array - the array to write
        pathToFile - the path to the file