File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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: 3677

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.
I agree. Here's the link:
subject: BufferedReader mark() and reset() help