org.esa.beam.util.io
Class CsvReader

java.lang.Object
  extended by java.io.Reader
      extended by java.io.BufferedReader
          extended by java.io.LineNumberReader
              extended by org.esa.beam.util.io.CsvReader
All Implemented Interfaces:
Closeable, Readable

public class CsvReader
extends LineNumberReader

A CsvReader 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
Author:
Norman Fomferra

Field Summary
 
Fields inherited from class java.io.Reader
lock
 
Constructor Summary
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
 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, markSupported, ready
 
Methods inherited from class java.io.Reader
read, read
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

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 wrapped
separators - 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 wrapped
separators - the column separator characters
ignoreEmptyLines - if true, empty lines are ignored
commentPrefix - if not null, 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 the readLineRecord 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


Copyright © 2002-2012 Brockmann Consult GmbH. All Rights Reserved.