Package org.tinfour.io
Class DelimitedReader
- java.lang.Object
-
- org.tinfour.io.DelimitedReader
-
- All Implemented Interfaces:
Closeable,AutoCloseable
public class DelimitedReader extends Object implements Closeable
A utility for reading a file consisting of strings separated by a delimiter character, such as the classic comma-separated-value (CSV) or tab-delimited file formats. Strings are extracted with leading and trailing white space removed. Embedded white space characters are preserved.
-
-
Constructor Summary
Constructors Constructor Description DelimitedReader(File file, char delimiter)Open a file and prepare to read it using the specified delimiter character.DelimitedReader(InputStream ins, char delimiter)Prepares to read an input stream using the specified delimiter character.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()CLoses all open IO channels.intgetLineNumber()Gets the current position in the file as a line number.List<String>readStrings()Read a row of strings from the fileintreadStrings(List<String> sList)Read a row of strings from the file, storing the results in a reusable list.
-
-
-
Constructor Detail
-
DelimitedReader
public DelimitedReader(File file, char delimiter) throws IOException
Open a file and prepare to read it using the specified delimiter character.- Parameters:
file- a valid file referencedelimiter- a delimiter character- Throws:
IOException- in the event of an unsuccessful I/O operation
-
DelimitedReader
public DelimitedReader(InputStream ins, char delimiter) throws IOException
Prepares to read an input stream using the specified delimiter character.- Parameters:
ins- a valid input stream referencedelimiter- a delimiter character- Throws:
IOException- in the event of an unsuccessful I/O operation
-
-
Method Detail
-
readStrings
public List<String> readStrings() throws IOException
Read a row of strings from the file- Returns:
- a list of strings (potentially empty)
- Throws:
IOException- in the event of an unsuccessful I/O operation.
-
readStrings
public int readStrings(List<String> sList) throws IOException
Read a row of strings from the file, storing the results in a reusable list. Each call to this routine clears any content that may already be in the list before extracting it from the file.- Parameters:
sList- a list in which the strings will be stored- Returns:
- the number of strings that were stored in the list; zero at the end of the file.
- Throws:
IOException- in the event of an unsuccessful I/O operation.
-
close
public void close() throws IOExceptionCLoses all open IO channels.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException- in the event of an unexpected I/O condition.
-
getLineNumber
public int getLineNumber()
Gets the current position in the file as a line number.- Returns:
- a value from 1 to the number of lines in the file.
-
-