Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.help > #605

Re: Using mark and reset on BufferReader, but with unknown file length

From Eric Sosman <esosman@ieee-dot-org.invalid>
Newsgroups comp.lang.java.help
Subject Re: Using mark and reset on BufferReader, but with unknown file length
Date 2011-04-23 08:52 -0400
Organization A noiseless patient Spider
Message-ID <ioui22$v07$1@dont-email.me> (permalink)
References <87mxjhrz10.fsf@merciadriluca-station.MERCIADRILUCA>

Show all headers | View raw


On 4/23/2011 7:24 AM, Merciadri Luca wrote:
>
> For some reasons that come out of the scope of this discussion, I need
> to read a file twice. I would prefer not to do
>
> BufferedReader inputFile = new BufferedReader(new
> FileReader(inputFilename));
>
> twice, and, as a result, I would like to use my preceding
> BufferedReader to read the file again, a second time.
>
> That is, I first read the file, then close it, then need to re-read
> it. To re-read it, I would like to use reset and mark methods, defined
> over BufferedReader objects. The problem is that mark obliges me to
> give a number of characters:
>
> ==
> readAheadLimit - Limit on the number of characters that may be read while still preserving the mark. After reading this many characters, attempting to reset the stream may fail. A limit value larger than the size of the input buffer will cause a new buffer to be allocated whose size is no smaller than limit. Therefore large values should be used with care.
> ==
> Unfortunately, I don't know how much characters there will be in my
> file, and need to get to the beginning of it for reading it the second
> time. Could I use reset() without mark? And, as I can't type a big
> integer as readAheadLimit (because it would allocate too much memory), how can I do?

     You cannot use reset() without a prior mark().  (Well, you *can*,
but it's just a fancy way to throw an IOException.)  What reset() does
is "rewind" to the mark() position; if there has been no mark() there
is no position to rewind to.

     If your input is re-readable (e.g. a file that's not being changed
by some other activity), opening a fresh Reader is a straightforward
and simple way to re-read it.  Alternatively, you could try mark() with
whatever size you're comfortable with, hoping and trusting that the
BufferedReader will exploit the source's re-readability.

     If the input is not re-readable (keyboard, socket, microphone, ...),
then the only way to make a second pass over the data is to store it
on the first pass -- in short, a full-size buffer or other repository
is mandatory.

-- 
Eric Sosman
esosman@ieee-dot-org.invalid

Back to comp.lang.java.help | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Using mark and reset on BufferReader, but with unknown file length Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> - 2011-04-23 13:24 +0200
  Re: Using mark and reset on BufferReader, but with unknown file length Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-04-23 08:52 -0400
  Re: Using mark and reset on BufferReader, but with unknown file length Lothar Kimmeringer <news200709@kimmeringer.de> - 2011-04-26 09:50 +0200

csiph-web