aspose file tools
The moose likes I/O and Streams and the fly likes Line based access with RandomAccessFile Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Line based access with RandomAccessFile" Watch "Line based access with RandomAccessFile" New topic
Author

Line based access with RandomAccessFile

Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
Hi
Is there some sort of abstraction available over RandomAccessFile that let me jump(seek) to a specified line number in the file.
Any help appreciated!


Thanks and Regards
Andrey Kozhanov
Ranch Hand

Joined: Mar 12, 2010
Posts: 79
I believe you have to write your own class. I would extend RandomAccessFile and implement CharSequence, and then use this class in Pattern and Matcher to find EOL sequence.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3884
    
  18

This isn't very useful, because the point of a RandomAccessFile is to provide, well, random access. If you need to find the number of lines in a file, whether you do this with a Pattern or otherwise, you still end up reading the entire file up to the point you want to jump to.

Simply wrap the file in a Scanner or a BufferedReader or something.


The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2351
    
    8
Its only useful if the size of each line is a fixed length. Then in this case you can randomly read line.
 
Consider Paul's rocket mass heater.
 
subject: Line based access with RandomAccessFile
 
It's not a secret anymore!