If you can't store the contents in memory the best option is to use a temporary file. In short:
1) create a temporary file (perhaps using File.createTempFile)
2) read a line from the original file, modify as needed, write it to the temporary file
3) repeat step 2) until done
4) delete the original file
5) rename the temporary file to the original file (renameTo will also move files if the parent folder is different)
Not like this. First of all, you'll need the File returned by File.createTempFile("temporary","bh",file), as this is the file you're going to write to, not the original file. Haven't you read my first post? It basically includes the entire algorithm in normal words; all you need to do is translate it into code (hint: steps 2 and 3 are a loop).
subject: editing a line of a txt file with a thousands of line