Class FixedLengthCSVReader


  • public class FixedLengthCSVReader
    extends CSVReader
    This class extends the CSVReader and parses a CSV InputStream to a list of maps using fixed lengths for the columns. That means the CSV stream is not separated by field separators like ';', instead each field/column has a fixed position in line. Only full line comments (if activated) and empty lines are considered.

    After instantiating the parser you need to set the positions of the columns within a line.
    String testString = "wert1wert2foowert3\nwert4wert5foowert6";
    FixedLengthCSVReader r = new FixedLengthCSVReader( testString );
    //adding fixed field from position 0 to 4 and assigning it to column 0 (entry number in resulting map)
    r.addField( 0, 4, 0 );
    //adding fixed field from position 5 to 9 and assigning it to column 1
    r.addField( 5, 9, 1 );
    //adding fixed field from position 13 to 17 and assigning it to column 3
    r.addField( 13, 17, 3 );
    //Pasing first line
    Map line = r.getLine();

    • Constructor Detail

      • FixedLengthCSVReader

        public FixedLengthCSVReader​(java.lang.String fileName,
                                    java.lang.String encoding)
                             throws java.io.UnsupportedEncodingException,
                                    java.io.FileNotFoundException
        Opens the file with the given filename and sets the given encoding.
        Parameters:
        fileName - the filename of the CSV file
        encoding - the given encoding, default is "UTF-8"
        Throws:
        java.io.UnsupportedEncodingException - thrown by false encoding type
        java.io.FileNotFoundException - thrown if file not found
      • FixedLengthCSVReader

        public FixedLengthCSVReader​(java.io.File file,
                                    java.lang.String encoding)
                             throws java.io.UnsupportedEncodingException,
                                    java.io.FileNotFoundException
        Opens the file and sets the given encoding.
        Parameters:
        file - the CSV file
        encoding - the given encoding, default is "UTF-8"
        Throws:
        java.io.UnsupportedEncodingException - thrown by false encoding type
        java.io.FileNotFoundException - thrown if file not found
      • FixedLengthCSVReader

        public FixedLengthCSVReader​(java.io.InputStream is,
                                    java.lang.String encoding)
                             throws java.io.UnsupportedEncodingException
        Opens the given Inputstream with the given file.
        Parameters:
        is - the InputStream
        encoding - the given encoding
        Throws:
        java.io.UnsupportedEncodingException
      • FixedLengthCSVReader

        public FixedLengthCSVReader​(java.io.Reader reader)
        Opens the given reader. The default encoding is set to "UTF-8"
        Parameters:
        reader - the reader
      • FixedLengthCSVReader

        public FixedLengthCSVReader​(java.lang.String lines)
        A convenience constructor for passing csv data as simple string object.
        Parameters:
        lines - the csv data as string
    • Method Detail

      • setFieldTrimming

        public void setFieldTrimming​(boolean trim)
        Sets if field content should be trimmed while reading
        Parameters:
        trim - will be trimming enabled?
      • addField

        public void addField​(int startPosition,
                             int endPosition,
                             int columnNumber)
        Adds a field information to the reader. This information will be considered while parsing. An information consists of the fixed start and end position of the field within the records, as well as the position of the column at the resulting line object when calling parseLine. Interleaving of fields with respect to their start and end position is not allowed and will be checked. Furthermore adding an information for an already existing column number is not allowed.
        Parameters:
        startPosition - absolute start position of the field (starting at 0)
        endPosition - absolute end position of the field (position of the last character)
        columnNumber - number of the parsed field in line map (see parseLine
        Throws:
        java.lang.IllegalArgumentException - field interleaves with an existing one or number of column still exists
      • parseLine

        protected java.util.Map parseLine​(java.lang.String line)
        Description copied from class: CSVReader
        Tokenises the given line and returns a Map with following content:

        Map{
        { 0:Integer, Field_1:String },
        { 1:Integer, Field_2:String },
        ...
        { n-1:Integer, Field_n:String }
        }

        Overrides:
        parseLine in class CSVReader
        Parameters:
        line - the line
        Returns:
        a map with the parsed CSV fields or null if failure