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 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 Android Security Essentials Live Lessons 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: 18541

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: 19654

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
Don't get me started about those stupid light bulbs.
subject: Read from file without loading into the JVM?
Similar Threads
How get full Stream of document into other InputStream?
Adding attributes to root tag
Accessing hashmaps directly from disk instead of memory
Update file content without rewrite the whole file issue