Class CsvFileExtensions


  • public final class CsvFileExtensions
    extends java.lang.Object
    Utility class for the use of cvs-files.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.util.List<java.lang.String> formatKommaSeperatedFileToList​(java.io.File input, java.lang.String encoding)
      Reads every line from the File splits the data through a comma and puts them to the List.
      static void formatToCSV​(java.io.File input, java.io.File output, java.lang.String encoding)
      Formats a file that has in every line one input-data into a csv-file.
      static java.util.List<java.util.Map<java.lang.String,​java.lang.String>> getCvsAsListMap​(java.io.File input)
      Gets the given cvs file as list of maps.
      static java.util.List<java.util.Map<java.lang.String,​java.lang.String>> getCvsAsListMap​(java.io.File input, java.lang.String encoding)
      Gets the given cvs file as list of maps.
      static java.lang.String[] getDataFromLine​(java.lang.String line, java.lang.String seperator)
      Gets the data from line.
      static java.lang.String[] getDataFromLine​(java.lang.String line, java.lang.String seperator, boolean trim)
      Gets the data from line.
      static int getLineCountFromCsvFile​(java.io.File file)
      Gets the line count from csv file.
      static java.util.List<java.lang.String> readDataFromCVSFileToList​(java.io.File input, int position, boolean putFirstLine, java.lang.String encoding)
      Reads from a csv-file the field from the given position and puts them to the List.
      static java.util.List<java.lang.String> readDataFromCVSFileToList​(java.io.File input, int position, boolean putFirstLine, java.lang.String splitChar, java.lang.String encoding)
      Reads from a csv-file the field from the given position and puts them to the List.
      static java.util.Properties readFilelistToProperties​(java.io.File input)
      Read filelist to properties.
      static java.util.List<java.lang.String> readFileToList​(java.io.File file)
      Reads every line from the given File into a List and returns the List.
      static java.util.List<java.lang.String> readFileToList​(java.io.File file, java.lang.String encoding)
      Reads every line from the given File into a List and returns the List.
      static java.util.List<java.lang.String[]> readFileToList​(java.io.File file, java.lang.String seperator, java.lang.String encoding)
      Reads every line from the given File into a List of String arrays and returns the List.
      static java.util.List<java.lang.String> readLinesInList​(java.io.File input, java.lang.String encoding)
      Reads every line from the File and puts them to the List.
      static java.lang.String[] sortData​(java.io.File csvData, java.lang.String encoding)
      Read an csv-file and puts them in a String-array.
      static void storeFilelistToProperties​(java.io.File output, java.io.File input, java.lang.String comments)
      Stores a komma seperated file to a properties object.
      static void writeLines​(java.io.File output, java.util.Set<java.lang.String> values, java.lang.String encoding)
      Writes the toString() value of each item in a collection to the specified File line by line.
      static void writeLinesToFile​(java.util.Collection<java.lang.String> collection, java.io.File output, java.lang.String encoding)
      Writes all the String-object in the collection into the given file.
      • Methods inherited from class java.lang.Object

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

      • formatKommaSeperatedFileToList

        public static java.util.List<java.lang.String> formatKommaSeperatedFileToList​(java.io.File input,
                                                                                      java.lang.String encoding)
                                                                               throws java.io.IOException
        Reads every line from the File splits the data through a comma and puts them to the List.
        Parameters:
        input - The File from where the input comes.
        encoding - The encoding from the file.
        Returns:
        The List with all lines from the file.
        Throws:
        java.io.IOException - Signals that an I/O exception has occurred.
      • formatToCSV

        public static void formatToCSV​(java.io.File input,
                                       java.io.File output,
                                       java.lang.String encoding)
                                throws java.io.IOException
        Formats a file that has in every line one input-data into a csv-file.
        Parameters:
        input - The input-file to format. The current format from the file is every data in a line.
        output - The file where the formatted data should be inserted.
        encoding - the encoding
        Throws:
        java.io.IOException - When an io error occurs.
      • getCvsAsListMap

        public static java.util.List<java.util.Map<java.lang.String,​java.lang.String>> getCvsAsListMap​(java.io.File input)
                                                                                                      throws java.io.IOException
        Gets the given cvs file as list of maps. Every map has as key the header from the column and the corresponding value for this line.
        Parameters:
        input - the input
        Returns:
        the cvs as list map
        Throws:
        java.io.IOException - Signals that an I/O exception has occurred.
      • getCvsAsListMap

        public static java.util.List<java.util.Map<java.lang.String,​java.lang.String>> getCvsAsListMap​(java.io.File input,
                                                                                                             java.lang.String encoding)
                                                                                                      throws java.io.IOException
        Gets the given cvs file as list of maps. Every map has as key the header from the column and the corresponding value for this line.
        Parameters:
        input - the input
        encoding - the encoding to read
        Returns:
        the cvs as list map
        Throws:
        java.io.IOException - Signals that an I/O exception has occurred.
      • getDataFromLine

        public static java.lang.String[] getDataFromLine​(java.lang.String line,
                                                         java.lang.String seperator)
        Gets the data from line.
        Parameters:
        line - the line
        seperator - the seperator
        Returns:
        the data from line
      • getDataFromLine

        public static java.lang.String[] getDataFromLine​(java.lang.String line,
                                                         java.lang.String seperator,
                                                         boolean trim)
        Gets the data from line.
        Parameters:
        line - the line
        seperator - the seperator
        trim - the trim
        Returns:
        the data from line
      • getLineCountFromCsvFile

        public static int getLineCountFromCsvFile​(java.io.File file)
                                           throws java.io.IOException
        Gets the line count from csv file.
        Parameters:
        file - the file
        Returns:
        the line count from csv file
        Throws:
        java.io.IOException - Signals that an I/O exception has occurred.
      • readDataFromCVSFileToList

        public static java.util.List<java.lang.String> readDataFromCVSFileToList​(java.io.File input,
                                                                                 int position,
                                                                                 boolean putFirstLine,
                                                                                 java.lang.String encoding)
                                                                          throws java.io.IOException
        Reads from a csv-file the field from the given position and puts them to the List.
        Parameters:
        input - The input-file.
        position - The position from the field.
        putFirstLine - Flag that tells if the first line will be put in the list.
        encoding - the encoding
        Returns:
        The list with the data.
        Throws:
        java.io.IOException - When a io-problem occurs.
      • readDataFromCVSFileToList

        public static java.util.List<java.lang.String> readDataFromCVSFileToList​(java.io.File input,
                                                                                 int position,
                                                                                 boolean putFirstLine,
                                                                                 java.lang.String splitChar,
                                                                                 java.lang.String encoding)
                                                                          throws java.io.IOException
        Reads from a csv-file the field from the given position and puts them to the List.
        Parameters:
        input - The input-file.
        position - The position from the field.
        putFirstLine - Flag that tells if the first line will be put in the list.
        splitChar - the split char
        encoding - the encoding
        Returns:
        The list with the data.
        Throws:
        java.io.IOException - When a io-problem occurs.
      • readFilelistToProperties

        public static java.util.Properties readFilelistToProperties​(java.io.File input)
                                                             throws java.io.IOException
        Read filelist to properties.
        Parameters:
        input - the input
        Returns:
        the properties
        Throws:
        java.io.IOException - Signals that an I/O exception has occurred.
      • readFileToList

        public static java.util.List<java.lang.String> readFileToList​(java.io.File file)
                                                               throws java.io.IOException
        Reads every line from the given File into a List and returns the List.
        Parameters:
        file - The file from where to read.
        Returns:
        The List with all lines from the File.
        Throws:
        java.io.IOException - When a io-problem occurs.
      • readFileToList

        public static java.util.List<java.lang.String> readFileToList​(java.io.File file,
                                                                      java.lang.String encoding)
                                                               throws java.io.IOException
        Reads every line from the given File into a List and returns the List.
        Parameters:
        file - The file from where to read.
        encoding - The encoding to read.
        Returns:
        The List with all lines from the File.
        Throws:
        java.io.IOException - When a io-problem occurs.
      • readFileToList

        public static java.util.List<java.lang.String[]> readFileToList​(java.io.File file,
                                                                        java.lang.String seperator,
                                                                        java.lang.String encoding)
                                                                 throws java.io.IOException
        Reads every line from the given File into a List of String arrays and returns the List.
        Parameters:
        file - The file from where to read.
        seperator - the seperator
        encoding - The encoding to read.
        Returns:
        The List with all lines from the File.
        Throws:
        java.io.IOException - Signals that an I/O exception has occurred.
      • readLinesInList

        public static java.util.List<java.lang.String> readLinesInList​(java.io.File input,
                                                                       java.lang.String encoding)
                                                                throws java.io.IOException
        Reads every line from the File and puts them to the List.
        Parameters:
        input - The File from where the input comes.
        encoding - The encoding from the file.
        Returns:
        The List with all lines from the file.
        Throws:
        java.io.IOException - When a io-problem occurs.
      • sortData

        public static java.lang.String[] sortData​(java.io.File csvData,
                                                  java.lang.String encoding)
                                           throws java.io.FileNotFoundException,
                                                  java.io.IOException
        Read an csv-file and puts them in a String-array.
        Parameters:
        csvData - The csv-file with the data.
        encoding - The encoding to read.
        Returns:
        The data from the csv-file as a String-array.
        Throws:
        java.io.FileNotFoundException - the file not found exception
        java.io.IOException - When an io-error occurs.
      • storeFilelistToProperties

        public static void storeFilelistToProperties​(java.io.File output,
                                                     java.io.File input,
                                                     java.lang.String comments)
                                              throws java.io.IOException
        Stores a komma seperated file to a properties object. As key is the number from the counter.
        Parameters:
        output - the output
        input - the input
        comments - the comments
        Throws:
        java.io.IOException - Signals that an I/O exception has occurred.
      • writeLines

        public static void writeLines​(java.io.File output,
                                      java.util.Set<java.lang.String> values,
                                      java.lang.String encoding)
                               throws java.io.IOException
        Writes the toString() value of each item in a collection to the specified File line by line. The default VM encoding and the default line ending will be used.
        Parameters:
        output - The File where the values to save.
        values - The Set with the values to put into the file.
        encoding - The encoding from the file.
        Throws:
        java.io.IOException - When a io-problem occurs.
      • writeLinesToFile

        public static void writeLinesToFile​(java.util.Collection<java.lang.String> collection,
                                            java.io.File output,
                                            java.lang.String encoding)
                                     throws java.io.IOException
        Writes all the String-object in the collection into the given file.
        Parameters:
        collection - The collection with the String-object.
        output - The file where the String-object will be writing.
        encoding - The encoding from the file.
        Throws:
        java.io.IOException - Signals that an I/O exception has occurred.