Java RandomAccessFile move file pointer
import java.io.RandomAccessFile; public class Main { public static void main(String[] args) throws Exception { // Open the file in read-write mode RandomAccessFile raf = new RandomAccessFile("Main.java", "rw"); int counter = raf.readInt(); long currentPosition = raf.getFilePointer(); // Set the file pointer in the beginning raf.seek(0);// w ww . j ava2s.c om // Read the counter and increment it by 1 counter = raf.readInt(); counter++; // Set the file pointer to zero again to overwrite the value of the counter raf.seek(0); raf.writeInt(counter); // Restore the file pointer raf.seek(currentPosition); raf.close(); } }