Package org.esa.snap.core.util.io
Class CsvReader
- java.lang.Object
-
- java.io.Reader
-
- java.io.BufferedReader
-
- java.io.LineNumberReader
-
- org.esa.snap.core.util.io.CsvReader
-
- All Implemented Interfaces:
Closeable
,AutoCloseable
,Readable
public class CsvReader extends LineNumberReader
ACsvReader
instance is used read text files with CSV (comma separated values) format.This reader interprets each line of a text file as record (also empty lines). The fields of the record are delimited by a single separator character which must not necessarily be a comma.
- Version:
- 1.1
-
-
Constructor Summary
Constructors Constructor Description CsvReader(Reader reader, char[] separators)
Constructs a CSV reader using the given column separator characters.CsvReader(Reader reader, char[] separators, boolean ignoreEmptyLines, String commentPrefix)
Constructs a CSV reader using the given column separator characters and the format properties.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description String
getCommentPrefix()
Gets the comment prefix string (if any).char[]
getSeparators()
boolean
ignoresEmptyLines()
double[]
readDoubleRecord()
List<double[]>
readDoubleRecords()
String[]
readRecord()
Reads a record info from the database.List<String[]>
readStringRecords()
Reads the complete file from the current position on.-
Methods inherited from class java.io.LineNumberReader
getLineNumber, mark, read, read, readLine, reset, setLineNumber, skip
-
Methods inherited from class java.io.BufferedReader
close, lines, markSupported, ready
-
Methods inherited from class java.io.Reader
nullReader, read, read, transferTo
-
-
-
-
Constructor Detail
-
CsvReader
public CsvReader(Reader reader, char[] separators)
Constructs a CSV reader using the given column separator characters. The new reader will not ignore empty lines and does not recognize comment lines.- Parameters:
reader
- the underlying reader to be wrappedseparators
- the column separator characters
-
CsvReader
public CsvReader(Reader reader, char[] separators, boolean ignoreEmptyLines, String commentPrefix)
Constructs a CSV reader using the given column separator characters and the format properties.- Parameters:
reader
- the underlying reader to be wrappedseparators
- the column separator charactersignoreEmptyLines
- iftrue
, empty lines are ignoredcommentPrefix
- if notnull
, the prefix string for comment lines
-
-
Method Detail
-
getSeparators
public final char[] getSeparators()
- Returns:
- The column separator characters.
-
ignoresEmptyLines
public final boolean ignoresEmptyLines()
- Returns:
- Are empty lines ignored?
-
getCommentPrefix
public final String getCommentPrefix()
Gets the comment prefix string (if any).- Returns:
- The comment prefix string, if any.
null
otherwise.
-
readRecord
public String[] readRecord() throws IOException
Reads a record info from the database. A record is represented by a complete line in the CSV formatted text file.Leading and trailing whitespaces removed from each column value. For empty lines, the method returns an array of the length zero. The method returns
null
if the end of file has been reached.- Returns:
- a record containing the tokens delimited by the separator character passed to the constructor
- Throws:
IOException
- if an I/O error occurs
-
readDoubleRecord
public double[] readDoubleRecord() throws IOException
- Throws:
IOException
-
readStringRecords
public List<String[]> readStringRecords() throws IOException
Reads the complete file from the current position on. If thereadLineRecord
has not previously been called the method reads all records from the beginning of the file. For empty files, the method returns an vector having a zero size.- Returns:
- an vector of
String[]
records containing the tokens delimitted by the separator character passed to the constructor - Throws:
IOException
- if an I/O error occurs
-
readDoubleRecords
public List<double[]> readDoubleRecords() throws IOException
- Throws:
IOException
-
-