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 buffer whole file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "buffer whole file" Watch "buffer whole file" New topic

buffer whole file

Karsten Daemen

Joined: Nov 19, 2009
Posts: 19

I want to buffer a reasonable small file completely in a BufferedReader, so i can quickly prints it's output to different output streams. So i wrote this:

this throws:

I have now idea what i'm doing wrong. The file is only 2415 bytes long so the casting to integer can't be a problem I think ...

Give me golf clubs, fresh air and a beautiful partner, and you can keep the clubs and the fresh air.
Rob Spoor

Joined: Oct 27, 2005
Posts: 19649

Why not store the contents in a StringBuilder instead?

How To Ask Questions How To Answer Questions
Henry Wong

Joined: Sep 28, 2004
Posts: 18500

The exception means that the mark has been invalidated prior to the reset. And from looking at the source, it looks like the mark is invalidated at the instant that you fill up the buffer; you don't even have to read pass it.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
I agree. Here's the link:
subject: buffer whole file
Similar Threads
Out of Memory exception
Reading a file using NIO
Opening a PDF
Is there an efficient way to read a text file into a string?
Size problem in http message?