views:

582

answers:

3
+2  Q: 

Java File Cursor

Hi,

Is there some library for using some sort of cursor over a file? I have to read big files, but can't afford to read them all at once into memory. I'm aware of java.nio, but I want to use a higher level API.

A little backgrond: I have a tool written in GWT that analyzes submitted xml documents and then pretty prints the xml, among other things. Currently I'm writing the pretty printed xml to a temp file (my lib would throw me an OOMException if I use plain Strings), but the temp file's size are approaching 18 megs, I can't afford to respond a GWT RPC with 18 megs :)

So I can have a widget to show only a portion of the xml (check this example), but I need to read the corresponding portion of the file.

+2  A: 

Maybe java.io.RandomAccessFile can be of use to you. See: http://java.sun.com/j2se/1.4.2/docs/api/java/io/RandomAccessFile.html

johanneslink
With large file sizes as the OP is working with, java.nio is the way to go.
Stu Thompson
+4  A: 

Have you taken a look at using FileChannels (i.e., memory mapped files)? Memory mapped files allow you to manipulate large files without bringing the entire file into memory.

Here's a link to a good introduction: http://www.developer.com/java/other/article.php/1548681

toddk
You beat me to the punch! Yea, this is the way to go...
Stu Thompson
A: 

I don't understand when you ask for a "higher level API" when positioning the file pointer. It is the higher levels that may need to control the "cursor". If you want control, go lower, not higher.

I am certain that lower level Java io clases allow you to position yourself anywhere within any sized file without reading anything into memory until you want to. I know I have done it before. Try RandomAccessFile as one example.

dongilmore