|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectbcds.tools.DataFileReader
public class DataFileReader
Implements a line oriented file reader. It can read both text files and GZIP files, which are decompressed on the fly, transparently to the caller. The following tranformations are performed on the input:
Internally, the input is always read through an instace of java.io.LineNumberReader.
NOTE: It would have been good for this class to extend
java.io.Reader, but doing that would make implementing
the method rewind()
impossible: there is no
guarantee that reset works (especially on a user-provided
Reader object) and reopening the
file (as it is done now) would imply assigning ourselves a new instance,
which is impossible.
Constructor Summary | |
---|---|
DataFileReader(java.io.Reader ips)
Creates an instace for reading from ips with a default
buffer size of 32 KB. |
|
DataFileReader(java.io.Reader ips,
int buff_sz)
Creates an instace for reading from ips with the given
buffer size. |
|
DataFileReader(java.lang.String fname)
Creates an instance for reading the file named fname . |
Method Summary | |
---|---|
java.lang.String |
getFileName()
Returns the value passed to the constructor that accepts a filename parameter, or null if anoother constructor was used. |
int |
getLineNum()
Returns the line number of the last line read. |
java.io.Reader |
getReader()
Returns the Reader instance used by this object. |
static java.io.Reader |
newFileReader(java.lang.String fname)
Returns a new Reader instance. |
java.lang.String |
nextLine()
Returns the next available line, after performing the transformations on the input. |
void |
rewind()
Repositions the file pointer to the beginning. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public DataFileReader(java.lang.String fname)
fname
.
The internal reader object is creted through
newFileReader(String)
.
AnyException
- as indicated in newFileReader(String)
.public DataFileReader(java.io.Reader ips)
ips
with a default
buffer size of 32 KB.
public DataFileReader(java.io.Reader ips, int buff_sz)
ips
with the given
buffer size.
Method Detail |
---|
public static java.io.Reader newFileReader(java.lang.String fname)
fname
is the single charcter "-", an instance of
java.io.InputStreamReader bound to System.in
is returned.
If fname
seems to be a GZIP file (ends in ".gz", ignoring case),
it is opened with java.util.zip.GZIPInputReader.
If that fails, a message is sent to System.err indicating
that an attempt fill be made to open the file as a "plain" file.
If the file ends up being treated as "plain", an instance of java.io.FileReader is returned.
AnyException
- wrapping an IOException if any IO
error occurs (including FileNotFOundException).public void rewind()
Reader
object,
Reader.reset()
is tried on it and an IOException is thrown if that fails.
Note that the Reader object used internally is
exposed (see getReader()
), so that the caller can use
Reader.mark(int)
to change the target of a reset.
public java.io.Reader getReader()
Reader
instance used by this object.
This is always an instance of java.io.LineNumberReader
which wraps the user-provided Reader object if the
instantiation was performed without a filename.
When reading from a file, the reference returned is valid only
until the next rewind, as a new reader instance
is created on each call.
public int getLineNum()
public java.lang.String getFileName()
public java.lang.String nextLine()
The line counter is incremented.
Any IOException is turned into an AnyException. This is deliberate, so that the caller does not need to put try..catch everywhere.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |