Package de.hybris.platform.util
Class FixedLengthCSVReader
java.lang.Object
de.hybris.platform.util.CSVReader
de.hybris.platform.util.FixedLengthCSVReader
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 Summary
ConstructorsConstructorDescriptionFixedLengthCSVReader(File file, String encoding) Opens the file and sets the given encoding.FixedLengthCSVReader(InputStream is, String encoding) Opens the given Inputstream with the given file.FixedLengthCSVReader(Reader reader) Opens the given reader.FixedLengthCSVReader(String lines) A convenience constructor for passing csv data as simple string object.FixedLengthCSVReader(String fileName, String encoding) Opens the file with the given filename and sets the given encoding. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddField(int startPosition, int endPosition, int columnNumber) Adds a field information to the reader.protected MapTokenises the given line and returns aMapwith following content:
Map{
{ 0:Integer, Field_1:String },
{ 1:Integer, Field_2:String },
...
{ n-1:Integer, Field_n:String }
}
voidsetFieldTrimming(boolean trim) Sets if field content should be trimmed while readingMethods inherited from class de.hybris.platform.util.CSVReader
applyDecorators, clearAllCellDecorators, clearCellDecorator, close, closeQuietly, finished, getCellDecorator, getCommentOut, getCurrentLineNumber, getDecoratorMap, getFieldSeparator, getLine, getSourceLine, getTextSeparator, hasCellDecorators, isCommentedOut, isFinished, isMultiLineMode, isReading, isShowComments, markFinished, mustSkip, notifyNextLine, parse, parse, parse, parse, readNextLine, readSrcLineFromStream, setCellDecorator, setCommentOut, setFieldSeparator, setLinesToSkip, setMaxBufferLines, setMultiLineMode, setShowComments, setTextSeparator, trim
-
Constructor Details
-
FixedLengthCSVReader
public FixedLengthCSVReader(String fileName, String encoding) throws UnsupportedEncodingException, FileNotFoundException Opens the file with the given filename and sets the given encoding.- Parameters:
fileName- the filename of the CSV fileencoding- the given encoding, default is"UTF-8"- Throws:
UnsupportedEncodingException- thrown by false encoding typeFileNotFoundException- thrown if file not found
-
FixedLengthCSVReader
public FixedLengthCSVReader(File file, String encoding) throws UnsupportedEncodingException, FileNotFoundException Opens the file and sets the given encoding.- Parameters:
file- the CSV fileencoding- the given encoding, default is"UTF-8"- Throws:
UnsupportedEncodingException- thrown by false encoding typeFileNotFoundException- thrown if file not found
-
FixedLengthCSVReader
Opens the given Inputstream with the given file.- Parameters:
is- the InputStreamencoding- the given encoding- Throws:
UnsupportedEncodingException
-
FixedLengthCSVReader
Opens the given reader. The default encoding is set to"UTF-8"- Parameters:
reader- the reader
-
FixedLengthCSVReader
A convenience constructor for passing csv data as simple string object.- Parameters:
lines- the csv data as string
-
-
Method Details
-
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 callingparseLine. 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 (seeparseLine- Throws:
IllegalArgumentException- field interleaves with an existing one or number of column still exists
-
parseLine
Description copied from class:CSVReaderTokenises the given line and returns aMapwith following content:
Map{
{ 0:Integer, Field_1:String },
{ 1:Integer, Field_2:String },
...
{ n-1:Integer, Field_n:String }
}
-