public class CSVReader
extends java.lang.Object
Use of this CSVReader:
csvreader = new CSVReader(inputStream, null ); //null for default encodingcsvreader.setTextSeparator('?');commentOut = '#' //chars for comment linesfieldseparator = ';' //separate the CSV fieldstextseparator = '\"' //enclose a CSV textshowComments = false //all comment line are ignoredtoSkip = 0 //there will be no lines ignoredtoSkip is set to 3 the first 3 lines of data are skipped, regardless if they are empty
lines, comments, real header or data lines!
ArrayList i_am_the_parsed_csv_stream = new ArrayList();
while (csvreader.readNextLine())
{
i_am_the_parsed_csv_stream.add(csvreader.getLine());
}
After this while loop the ArrayList contains all parsed CSV lines. Each line is a Map of the parsed CSV
fields. See parseLine(String) for the setup of the map.csvreader.close() to close the reader.| Constructor and Description |
|---|
CSVReader(java.io.File file,
java.lang.String encoding)
Opens the file and sets the given encoding.
|
CSVReader(java.io.InputStream is,
java.lang.String encoding)
Opens the given Inputstream with the given file.
|
CSVReader(java.io.Reader reader)
Opens the given reader.
|
CSVReader(java.lang.String lines)
A convenience constructor for passing csv data as simple string object.
|
CSVReader(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 |
|---|---|
static java.util.Map |
applyDecorators(java.util.Map<java.lang.Integer,CSVCellDecorator> decoratorMap,
java.util.Map line)
Applies given decorators to columns in given line.
|
void |
clearAllCellDecorators()
Removes all declared cell decorators.
|
void |
clearCellDecorator(int position)
Removes one declared cell decorator.
|
void |
close()
Close the reader.
|
void |
closeQuietly()
Close the reader quietly.
|
boolean |
finished()
Is reading from stream is already finished (has stream reached its end)?
|
CSVCellDecorator |
getCellDecorator(int position)
Returns the decorator mapped to the given column position.
|
char[] |
getCommentOut() |
int |
getCurrentLineNumber() |
protected java.util.Map<java.lang.Integer,CSVCellDecorator> |
getDecoratorMap(boolean create)
Returns a map containing the current csv decorators mapped to column positions.
|
char[] |
getFieldSeparator() |
java.util.Map<java.lang.Integer,java.lang.String> |
getLine()
Returns the parsed line as map.
|
java.lang.String |
getSourceLine()
Gets the last line read from stream.
|
char |
getTextSeparator() |
boolean |
hasCellDecorators()
Are there declared cell decorators?
|
protected boolean |
isCommentedOut(java.lang.String line)
Returns true if the passed line is a comment (depends on
setCommentOut(char[])). |
boolean |
isFinished()
Is reading of input stream finished (has reached end)?
|
boolean |
isMultiLineMode()
Tells whether or not the reader supports csv lines spread across multiple lines by putting a
\
(backslash) at the end of each unfinished line. |
protected boolean |
isReading()
Checks if reader is already reading from stream.
|
boolean |
isShowComments() |
protected void |
markFinished()
Sets the finished flag which indicates the reaching of stream end.
|
protected boolean |
mustSkip()
Checks if lines have to be skipped still.
|
protected void |
notifyNextLine()
Increments the line number and decreases the line skip counter.
|
static java.util.Map<java.lang.Integer,java.lang.String>[] |
parse(CSVReader reader)
Convenience method which parses csv lines directly from the given reader.
|
static java.util.Map<java.lang.Integer,java.lang.String>[] |
parse(java.lang.String lines)
Convenience method which parses csv lines directly from the given string.
|
static java.util.Map<java.lang.Integer,java.lang.String>[] |
parse(java.lang.String lines,
char[] fieldSeparator)
Convenience method which parses csv lines directly from the given string.
|
static java.util.Map<java.lang.Integer,java.lang.String>[] |
parse(java.lang.String lines,
char[] fieldSeparator,
char textSeparator)
Convenience method which parses csv lines directly from the given string.
|
protected java.util.Map<java.lang.Integer,java.lang.String> |
parseLine(java.lang.String line)
Tokenises the given line and returns a
Map with following content: Map{ |
boolean |
readNextLine()
Reads and parses next line from stream and returns true if the line was read and parsed successfully.
|
protected java.lang.String |
readSrcLineFromStream()
Reads next line from stream.
|
void |
setCellDecorator(int position,
CSVCellDecorator decorator)
Maps a decorator to a column position.
|
void |
setCommentOut(char[] commentOut)
Set the characters which indicates a comment line.
|
void |
setFieldSeparator(char[] fieldseparator)
Sets the CSV field separator char(s).
|
void |
setLinesToSkip(int i)
Set the number of real lines which are skipped when
readNextLine() is called the first time. |
void |
setMaxBufferLines(int number)
Set
maxBufferLines to a new value. |
void |
setMultiLineMode(boolean on)
Changes whether or not the reader supports csv lines spread across multiple lines by putting a
\
(backslash) at the end of each unfinished line. |
void |
setShowComments(boolean showComments)
Set to true if all comment line should also parsed.
|
void |
setTextSeparator(char textseparator)
Sets the text separator char which enclose in CSV a text(default is
"). |
protected java.lang.String |
trim(java.lang.String src,
boolean fromStart,
boolean fromEnd)
Trims the given string like the trim() method of java.lang.String, but allows to disable trimming from start or
end of the string.
|
public CSVReader(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 CSVConstants.DEFAULT_ENCODINGjava.io.UnsupportedEncodingException - thrown by not supported encoding typejava.io.FileNotFoundException - thrown if file not foundpublic CSVReader(java.io.File file,
java.lang.String encoding)
throws java.io.UnsupportedEncodingException,
java.io.FileNotFoundException
file - the CSV fileencoding - the given encoding, default is CSVConstants.DEFAULT_ENCODINGjava.io.UnsupportedEncodingException - thrown by not supported encoding typejava.io.FileNotFoundException - thrown if file not foundpublic CSVReader(java.io.InputStream is,
java.lang.String encoding)
throws java.io.UnsupportedEncodingException
is - the InputStreamencoding - the given encoding, default is CSVConstants.DEFAULT_ENCODINGjava.io.UnsupportedEncodingException - thrown by not supported encoding typepublic CSVReader(java.io.Reader reader)
CSVConstants.DEFAULT_ENCODINGreader - the readerpublic CSVReader(java.lang.String lines)
lines - the csv data as stringprotected java.util.Map<java.lang.Integer,CSVCellDecorator> getDecoratorMap(boolean create)
create flag is set.create - If no cell decorators declared and flag is set a new map will be returnedpublic void clearAllCellDecorators()
public void clearCellDecorator(int position)
position - the column position the decorator is mappedpublic void setCellDecorator(int position,
CSVCellDecorator decorator)
position - position the decorator will be mappeddecorator - the decoratorpublic boolean hasCellDecorators()
public CSVCellDecorator getCellDecorator(int position)
position - position of column for which the decorator is neededprotected java.lang.String readSrcLineFromStream()
null is returned and finished flag will be
set.nullpublic boolean finished()
readNextLine has returned nullpublic final boolean readNextLine()
protected java.lang.String trim(java.lang.String src,
boolean fromStart,
boolean fromEnd)
src - the string which will be trimmedfromStart - is trimming from the left side of the string enabled?fromEnd - is trimming from the right side of the string enabled?String.trim()public java.util.Map<java.lang.Integer,java.lang.String> getLine()
public java.lang.String getSourceLine()
protected java.util.Map<java.lang.Integer,java.lang.String> parseLine(java.lang.String line)
Map with following content: Map{
{ 0:Integer, Field_1:String },
{ 1:Integer, Field_2:String },
...
{ n-1:Integer, Field_n:String }
}
line - the linenull if failurepublic void close()
throws java.io.IOException
java.io.IOException - throws if error occurredpublic void closeQuietly()
public void setTextSeparator(char textseparator)
"). If line was read already a
IllegalStateException will be thrown.textseparator - the text separator charpublic boolean isShowComments()
protected boolean isCommentedOut(java.lang.String line)
setCommentOut(char[])).line - the passed linepublic void setShowComments(boolean showComments)
showComments - default value is falseprotected boolean isReading()
public void setMultiLineMode(boolean on)
\
(backslash) at the end of each unfinished line.
An example:
cell 1 ; cell 2 ; cell 3 starts here \ ...and continues here ... \ ... finally ends here ; cell4This is read as one single line.
on - will multi line mode be enabled?public boolean isMultiLineMode()
\
(backslash) at the end of each unfinished line.
Be default this feature is switched off.
An example:
cell 1 ; cell 2 ; cell 3 starts here \ ...and continues here ... \ ... finally ends here ; cell4This is read as one single line.
public void setCommentOut(char[] commentOut)
commentOut - default characters are '#'public void setFieldSeparator(char[] fieldseparator)
fieldseparator - the char(s)public char[] getCommentOut()
public char[] getFieldSeparator()
public char getTextSeparator()
public void setLinesToSkip(int i)
readNextLine() is called the first time.i - must be a positive integer valuegetCurrentLineNumber()public int getCurrentLineNumber()
readNextLine() within the input data (file).protected void notifyNextLine()
protected boolean mustSkip()
public void setMaxBufferLines(int number)
maxBufferLines to a new value. Must be larger than 5.number - the numberpublic boolean isFinished()
protected void markFinished()
public static final java.util.Map<java.lang.Integer,java.lang.String>[] parse(CSVReader reader)
reader - the reader holding the lines to be readpublic static final java.util.Map<java.lang.Integer,java.lang.String>[] parse(java.lang.String lines,
char[] fieldSeparator,
char textSeparator)
lines - text which will be readfieldSeparator - e.g. ;textSeparator - e.g. "public static final java.util.Map<java.lang.Integer,java.lang.String>[] parse(java.lang.String lines,
char[] fieldSeparator)
lines - text which will be readfieldSeparator - e.g. ;public static final java.util.Map<java.lang.Integer,java.lang.String>[] parse(java.lang.String lines)
lines - text which will be readpublic static java.util.Map applyDecorators(java.util.Map<java.lang.Integer,CSVCellDecorator> decoratorMap, java.util.Map line)
decoratorMap - map containing the decorators mapped to column positionsline - map containing columns of a lineCopyright © 2018 SAP SE. All Rights Reserved.