CSCE155
FAQs and Interesting Questions
Q8. How to move the file pointer to skip reading unwanted input data?
A8. It is possible that when a program reads a
large file, it wants to skip reading the first N lines of data. Skipping
saves time. Just to think if the program
had to read all N lines of data just
to get to one particular line of a large input file, it would be very
inefficient. In Java, under the FileInputStream class (that we have already discussed), there is a method
called skip:
java.lang.Object
java.io.InputStream
java.io.FileInputStream
|
Method Summary |
|
|
|
|
There is
also another class called DataInputStream
(that we have already discussed), it has several methods to read specific data
types from an input file, in addition to a method that skips. These methods are overloaded. One overload allows the programmer to specify
an offset:
java.lang.Object
java.io.InputStream
java.io.FilterInputStream
java.io.DataInputStream
|
Method Summary |
|
|
|
|
|
|
|
|
|
|
But what about
moving the file pointer to a specific location?
Q9. How to move the file pointer to a specific location?
A9. There is one way. We use the methods mark()
and reset() that are readily available (inherited) from the java.io.FilterInputStream class (for the DataInputStream class) and the java.io.InputStream class (for the FileInputStream class). Basically,
you could mark a position in the input stream, and then reset to get back
to that position.
public void mark(int readlimit)
Marks the current position in this input
stream. A subsequent call to
the reset method
repositions this stream at the last marked position so that subsequent reads
re-read the same bytes. The readlimit argument tells this input stream to allow that many bytes to be read
before the mark position gets invalidated.
public void reset()
throws IOException
Repositions
this stream to the position at the time the mark method was last called on this input
stream. Stream marks are intended to be
used in situations where you need to read ahead a little to see what's in the
stream. Often this is most easily done by invoking some general parser. If the
stream is of the type handled by the parse, it just chugs along happily. If the
stream is not of that type, the parser should toss an exception when it fails.
If this happens within readlimit bytes, it allows the
outer code to reset the stream and try another parser.
So, in
short, there is not an easy way to move the file pointer around in a file, as
easy as one could do in C or C++.