• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Read from file without loading into the JVM?

 
Derek White
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21117
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20546
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic