public static void writeIntegerToStart ( final File f , final int num ) throws IOException { final RandomAccessFile fixupFile = new RandomAccessFile ( f , "rw" ) ; fixupFile . writeInt ( num ) ; fixupFile . close ( ) ; }
Writes a single integer to the beginning of a file overwriting what was there originally but leaving the rest of the file intact . This is useful when you are writing a long binary file with a size header but don t know how many elements are there until the end .