O'Reilly logo

Java™ Phrasebook by Timothy Fisher

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Seeking in a File

File file = new File("somefile.bin");
											RandomAccessFile raf =
											new RandomAccessFile(file, "rw");
											raf.seek(file.length());

Using the seek() method of the RandomAccessFile class, we can seek to any desired position within a file. In this phrase, we first create a File object, which is then used to create a RandomAccessFile instance. Using the RandomAccessFile instance, raf, we seek to the end of the file by passing in the file.length() value as a parameter to the seek() method.

After using the seek() method to find the desired position within a file, we can then use the read() or write() methods of the RandomAccessFile class to read or write data at that exact position.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required