• Post Reply Bookmark Topic Watch Topic
  • New Topic

Read BACK in a file  RSS feed

 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a requirement where I need to display data persisted in a file (a BIG file with millions of records) on the UI in a JTable.

I have a FileHandler class which does the actual file IO. The custom table model will query this class for the getValueAt() method.

When the user scrolls down, the model queries the FileHandler which calls the readLine() and everything is nice and fine.
However, when the user scrolls up, I run into a problem. How do I read the previous line?
I saw the skip() throws an IllegalArgumentException if I pass a -ve long as an argument. I also could not find anything useful in the LineNumberReader. The setLineNumber looked promising, but once I read the doc, I found it didnt really help much.

Only way out seems to be the read(char[] buff,int offset, int length) or the seek(long pos) in the RandomAccessFile.

Anyone else know a better solution?
I am trying to minimize the memory footprint. So storing the known row data in an array in the model is out of question.
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If all the records are the same size, RandomAccessFile would probably be best for your task because you can use seek to page back by a known amount (record size x # of records).
If the records vary in size and the file doesn't change much, it may be worthwhile to build an index of all the rows offsets in the file and use that to navigate your data (both using RandomAccessFile).
Java also supports memory-mapped files, which looks like it may be useful, but I've never used them.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joe,

Thanks for your pointers, especially the MappedByteBuffer. Will try it out.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!