public class CSVReader extends 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(File file,
String encoding)
Opens the file and sets the given encoding.
|
CSVReader(InputStream is,
String encoding)
Opens the given Inputstream with the given file.
|
CSVReader(Reader reader)
Opens the given reader.
|
CSVReader(String lines)
A convenience constructor for passing csv data as simple string object.
|
CSVReader(String fileName,
String encoding)
Opens the file with the given filename and sets the given encoding.
|
| Modifier and Type | Method and Description |
|---|---|
static Map |
applyDecorators(Map<Integer,CSVCellDecorator> decoratorMap,
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 Map<Integer,CSVCellDecorator> |
getDecoratorMap(boolean create)
Returns a map containing the current csv decorators mapped to column positions.
|
char[] |
getFieldSeparator() |
Map<Integer,String> |
getLine()
Returns the parsed line as map.
|
String |
getSourceLine()
Gets the last line read from stream.
|
char |
getTextSeparator() |
boolean |
hasCellDecorators()
Are there declared cell decorators?
|
protected boolean |
isCommentedOut(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 Map<Integer,String>[] |
parse(CSVReader reader)
Convenience method which parses csv lines directly from the given reader.
|
static Map<Integer,String>[] |
parse(String lines)
Convenience method which parses csv lines directly from the given string.
|
static Map<Integer,String>[] |
parse(String lines,
char[] fieldSeparator)
Convenience method which parses csv lines directly from the given string.
|
static Map<Integer,String>[] |
parse(String lines,
char[] fieldSeparator,
char textSeparator)
Convenience method which parses csv lines directly from the given string.
|
protected Map<Integer,String> |
parseLine(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 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 String |
trim(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(String fileName, String encoding) throws UnsupportedEncodingException, FileNotFoundException
fileName - the filename of the CSV fileencoding - the given encoding, default is CSVConstants.DEFAULT_ENCODINGUnsupportedEncodingException - thrown by not supported encoding typeFileNotFoundException - thrown if file not foundpublic CSVReader(File file, String encoding) throws UnsupportedEncodingException, FileNotFoundException
file - the CSV fileencoding - the given encoding, default is CSVConstants.DEFAULT_ENCODINGUnsupportedEncodingException - thrown by not supported encoding typeFileNotFoundException - thrown if file not foundpublic CSVReader(InputStream is, String encoding) throws UnsupportedEncodingException
is - the InputStreamencoding - the given encoding, default is CSVConstants.DEFAULT_ENCODINGUnsupportedEncodingException - thrown by not supported encoding typepublic CSVReader(Reader reader)
CSVConstants.DEFAULT_ENCODINGreader - the readerpublic CSVReader(String lines)
lines - the csv data as stringprotected Map<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 String readSrcLineFromStream()
null is returned and finished flag will be
set.nullpublic boolean finished()
readNextLine has returned nullpublic final boolean readNextLine()
protected String trim(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 String getSourceLine()
protected Map<Integer,String> parseLine(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 IOException
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(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 Map<Integer,String>[] parse(CSVReader reader)
reader - the reader holding the lines to be readpublic static final Map<Integer,String>[] parse(String lines, char[] fieldSeparator, char textSeparator)
lines - text which will be readfieldSeparator - e.g. ;textSeparator - e.g. "public static final Map<Integer,String>[] parse(String lines, char[] fieldSeparator)
lines - text which will be readfieldSeparator - e.g. ;public static final Map<Integer,String>[] parse(String lines)
lines - text which will be readpublic static Map applyDecorators(Map<Integer,CSVCellDecorator> decoratorMap, Map line)
decoratorMap - map containing the decorators mapped to column positionsline - map containing columns of a lineCopyright © 2017 SAP SE. All Rights Reserved.