Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ftell()/tellg()? (How to get current read position in input stream?)

147 views
Skip to first unread message

Andrew R. Thomas-Cramer

unread,
Oct 24, 2000, 3:00:00 AM10/24/00
to

Using the java.io API, how does one find the current read position, measured
in bytes, in an InputStream or Reader?

I see that RandomAccessFile has getFilePointer(), but don't see how to make
use of this in an InputStream or Reader, without defining my own Reader to
operate on a RandomAccessFile.

I also see InputStream.available(), which _might_ return the offset from the
end of the file (though this is not specified explicitly), but it returns an
int rather than a long -- so it couldn't return all offsets from the end of
a really large file.

My goal is to show the progress of reading a large file. I can find the
total length of the file in bytes with File.length(), and the current offset
in lines with a LineNumberReader, but don't know how to find (efficiently)
the current offset in bytes or the total number of lines.

============================================================================
Andrew R. Thomas-Cramer
Home: ar...@execpc.com http://www.execpc.com/~artc/
Work: ar...@prism-cs.com 608.287.1043
============================================================================


Brian O'Byrne

unread,
Oct 24, 2000, 3:00:00 AM10/24/00
to
Andrew R. Thomas-Cramer (ar...@prism-cs.com) wrote:
:
: Using the java.io API, how does one find the current read position, measured

: in bytes, in an InputStream or Reader?

InputStream and Reader are designed to be used in a stream of indefinite
or undefined length, so they do not have a concept of 'percentage
complete'. They also dont have a byte counter (of bytes read), probably
just because no-one considered it important enough to include in the spec.

: I see that RandomAccessFile has getFilePointer(), but don't see how to make


: use of this in an InputStream or Reader, without defining my own Reader to
: operate on a RandomAccessFile.
:
: I also see InputStream.available(), which _might_ return the offset from the
: end of the file (though this is not specified explicitly), but it returns an
: int rather than a long -- so it couldn't return all offsets from the end of
: a really large file.

available() will return the number of bytes ready to be read. Typically,
this is the number of bytes in the input buffer which you have not read.

: My goal is to show the progress of reading a large file. I can find the


: total length of the file in bytes with File.length(), and the current offset
: in lines with a LineNumberReader, but don't know how to find (efficiently)
: the current offset in bytes or the total number of lines.

I think you have found all there is to find. Implement a counter which
counts the number of bytes you have read so far and compare that with
File.length().
For fairly obvious reasons, you cant know the number of lines until after
the last line has been read.

Brian.


0 new messages