File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes RandomAccessFile Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "RandomAccessFile Help" Watch "RandomAccessFile Help" New topic

RandomAccessFile Help

Nitin Gupte

Joined: Sep 12, 2006
Posts: 6
I have a text file with a path of another file on each line. I have a Java code that reads this file searches for the file on the particular line and takes it's contents. But in case of an error, it writes the line number to a temporary file so i can start exactly where i left. My problem is that the seek() method in RandomAccessFile class skips the number of bytes specified, not line numbers..
Is there any method any where in Java APIs which i can use to skip the specified lines from reading?
I want to avoid looping.. currently i using a loop n i say continue till i've reached the line number. I hope all this is making sense
Joe Ess

Joined: Oct 29, 2001
Posts: 9189

Originally posted by Nitin Gupte:

Is there any method any where in Java APIs which i can use to skip the specified lines from reading?

No. Files are just bytes. Line termination characters are particular bytes that we've agreed mean something special. There's no way to distinguish line terminators from other bytes without reading all of the bytes in.
You could, if you really needed this kind of functionality, make a file with fixed-length records, each record being a line padded to the maximum length. Then you could find a particular line by multiplying the record length by the line number. This, of course, comes with its own set of problems, like what the max line length is, how do you enforce it, how to insert and delete lines and so on.

[How To Ask Questions On JavaRanch]
I agree. Here's the link:
subject: RandomAccessFile Help
It's not a secret anymore!