3

How can I seek into a text file in Java? I'm processing a text-based journal file, and would like to checkpoint my work so that it can be resumed by a future invocation of my program.

I'm looking for something similar to C stdio fseek()/ftell() functionality.

Mark Harrison
  • 297,451
  • 125
  • 333
  • 465
  • 1
    closely related, [java file input with rewind()/reset() capability]http://stackoverflow.com/questions/1094703/java-file-input-with-rewind-reset-capability) - for the generic byte stream case though, multi-byte characters can add quite a bit of complexity on top – zapl Nov 30 '15 at 22:05

1 Answers1

7

RandomAccessFile gives you random access to a file (surprise, surprise :)) and its readLine() method allows you to process it line by line.

The current position can be queried by getFilePointer() and the seek() method seeks.

biziclop
  • 48,926
  • 12
  • 77
  • 104