Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: Eric Sosman Newsgroups: comp.lang.java.help Subject: Re: Using mark and reset on BufferReader, but with unknown file length Date: Sat, 23 Apr 2011 08:52:16 -0400 Organization: A noiseless patient Spider Lines: 43 Message-ID: References: <87mxjhrz10.fsf@merciadriluca-station.MERCIADRILUCA> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sat, 23 Apr 2011 12:52:19 +0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="KiwfXDyOjqGhZBXcfNnZBg"; logging-data="31751"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+oFEcKTcbhxUJ8sCBgf7Al" User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.15) Gecko/20110303 Thunderbird/3.1.9 In-Reply-To: <87mxjhrz10.fsf@merciadriluca-station.MERCIADRILUCA> Cancel-Lock: sha1:/3Dzo+JI+NJ9n5SRxVrjHinp8w4= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.help:605 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