import java.io.RandomAccessFile; import java.io.FileNotFoundException; import java.io.IOException; public class RandomAccessFileDemo { public static void main(String[] args) { try { //Open the file for writing and reading. RandomAccessFile f = new RandomAccessFile("rand.txt", "rw"); f.writeBytes("Hello"); System.out.println("pointer == " + f.getFilePointer()); f.seek(0); System.out.println("pointer == " + f.getFilePointer()); byte[] a = new byte[5]; f.read(a); System.out.println(new String(a)); f.close(); } catch (FileNotFoundException e) { //file can't be created or opened System.err.println(e.getMessage()); } catch (IOException e) { System.err.println(e.getMessage()); } } }