my dog learned polymorphism
The moose likes I/O and Streams and the fly likes BufferedReader mark() and reset() help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "BufferedReader mark() and reset() help" Watch "BufferedReader mark() and reset() help" New topic

BufferedReader mark() and reset() help

Joe Smoe

Joined: Nov 12, 2010
Posts: 1

I'm trying to write a function that grabs a 4 character sequences of a file, sends that to another function, then continue to do the same thing from where the BufferedReader left off until the end of the file but can't seem to figure out how to make it work. Here is what I have so far:

Any help as to what I'm doing wrong is much appreciated.

Stephan van Hulst

Joined: Sep 20, 2010
Posts: 4637

You have a few problems. First of all, you don't need mark and reset. They are used to reread the same parts of a file.

Secondly, after you read your first sequence, the program will stop reading, because count is always 4. You don't reset it.

Thirdly, what happens when the file has less than 4 characters left at some point, but more than one? It will cast -1 to a character and append it to str. This will result in the last unicode character in existence.

Lastly, you might want to consider using a StringBuilder instead of a String. With a large file, the overhead of appending single characters to the end of a String may be very big.

The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
I agree. Here's the link:
subject: BufferedReader mark() and reset() help
It's not a secret anymore!