This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes Read from file without loading into the JVM? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Read from file without loading into the JVM?" Watch "Read from file without loading into the JVM?" New topic

Read from file without loading into the JVM?

Derek White

Joined: Jul 24, 2008
Posts: 14
Is it possible to read/write (using a stream maybe?) from a file without actually loading the whole thing into the JVM?

I am used to loading the whole file into memory but I realize if a file becomes to large it will hog resources. Is there any way I can maintain low memory usage and the ability to constantly read/write data?
Paul Clapham

Joined: Oct 14, 2005
Posts: 19691

Of course. Simply read part of the file, process it, then throw it away and read the next part of the file.

A common example of this is where you read a text file one line at a time and process each line separately.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20048

By default, FileInputStream and FileReader don't store more than a few kilobytes. Just keep on calling on of the read methods (or readLine() if you've wrapped them in a BufferedReader) and process the bytes / chars / lines directly instead of storing them.

How To Ask Questions How To Answer Questions
It is sorta covered in the JavaRanch Style Guide.
subject: Read from file without loading into the JVM?
It's not a secret anymore!