• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading File in Chunks

 
Amirr Rafique
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rancher

I have a File object that is representing a CSV file. I want to read that file in chunks e.g say first time i read 50 records and next time i want to jump at 51st record and read next till 100th. Is there any way to access the file data randomly. Please help

Thanks
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a RandomAccessFile in the JDK, but it will only read "records" conveniently if they are all the same length. If you can pad fields and/or records to a fixed length, it will work great. Otherwise, probably not what you need.

Do you need to close the file between chunks? If not, you can just read 50 lines, leave the file open while you do something else, read 50 more and so on. If yes, you might try to compute how many bytes you have read in the first chunk, close the file while you do something else, open it again and Reader.skip() that many bytes before reading more. The only tricky bit there might be counting CR and/or LF characters ... linends are not always the same size.

Any of that sound useful?
[ March 08, 2007: Message edited by: Stan James ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!