The first thing i would like to highlight in above program, file is opened in Read Mode, this will not allow user to modify the file.
To achieve the same you can use RandomAccessFile , but it is part of Java 7
http://docs.oracle.com/javase/7/docs/api/java/io/RandomAccessFile.html
Still the ideal solution will be given by Mr. Garth. Instead of over-writing the existing file it is always good copy the required contents in one temp file , delete the existing file, and then rename the temp file to original file.
Regards,
panduranga.