public class FixedLengthCSVReader extends CSVReader
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 and Description |
|---|
FixedLengthCSVReader(java.io.File file,
java.lang.String encoding)
Opens the file and sets the given encoding.
|
FixedLengthCSVReader(java.io.InputStream is,
java.lang.String encoding)
Opens the given Inputstream with the given file.
|
FixedLengthCSVReader(java.io.Reader reader)
Opens the given reader.
|
FixedLengthCSVReader(java.lang.String lines)
A convenience constructor for passing csv data as simple string object.
|
FixedLengthCSVReader(java.lang.String fileName,
java.lang.String encoding)
Opens the file with the given filename and sets the given encoding.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addField(int startPosition,
int endPosition,
int columnNumber)
Adds a field information to the reader.
|
protected java.util.Map |
parseLine(java.lang.String line)
Tokenises the given line and returns a
Map with following content: Map{ |
void |
setFieldTrimming(boolean trim)
Sets if field content should be trimmed while reading
|
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, trimpublic FixedLengthCSVReader(java.lang.String fileName,
java.lang.String encoding)
throws java.io.UnsupportedEncodingException,
java.io.FileNotFoundException
fileName - the filename of the CSV fileencoding - the given encoding, default is "UTF-8"java.io.UnsupportedEncodingException - thrown by false encoding typejava.io.FileNotFoundException - thrown if file not foundpublic FixedLengthCSVReader(java.io.File file,
java.lang.String encoding)
throws java.io.UnsupportedEncodingException,
java.io.FileNotFoundException
file - the CSV fileencoding - the given encoding, default is "UTF-8"java.io.UnsupportedEncodingException - thrown by false encoding typejava.io.FileNotFoundException - thrown if file not foundpublic FixedLengthCSVReader(java.io.InputStream is,
java.lang.String encoding)
throws java.io.UnsupportedEncodingException
is - the InputStreamencoding - the given encodingjava.io.UnsupportedEncodingExceptionpublic FixedLengthCSVReader(java.io.Reader reader)
"UTF-8"reader - the readerpublic FixedLengthCSVReader(java.lang.String lines)
lines - the csv data as stringpublic void setFieldTrimming(boolean trim)
trim - will be trimming enabled?public void addField(int startPosition,
int endPosition,
int columnNumber)
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.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 parseLinejava.lang.IllegalArgumentException - field interleaves with an existing one or number of column still existsprotected java.util.Map parseLine(java.lang.String line)
CSVReaderMap with following content: Map{
{ 0:Integer, Field_1:String },
{ 1:Integer, Field_2:String },
...
{ n-1:Integer, Field_n:String }
}
Copyright © 2018 SAP SE. All Rights Reserved.