How to read a file from a certain offset in Java?

Solution 1:

RandomAccessFile exposes a function:

seek(long pos) 
          Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs.

Solution 2:

FileInputStream.getChannel().position(123)

This is another possibility in addition to RandomAccessFile:

File f = File.createTempFile("aaa", null);
byte[] out = new byte[]{0, 1, 2};

FileOutputStream o = new FileOutputStream(f);
o.write(out);
o.close();

FileInputStream i = new FileInputStream(f);
i.getChannel().position(1);
assert i.read() == out[1];
i.close();
f.delete();

This should be OK since the docs for FileInputStream#getChannel say that:

Changing the channel's position, either explicitly or by reading, will change this stream's file position.

I don't know how this method compares to RandomAccessFile however.