I have a text file that I want to edit using Java. It has many thousands of lines. I basically want to iterate through the lines and change/edit/delete some text. This will need to happen quite often.
From the solutions I saw on other sites, the general approach seems to be:
- Open the existing file using a BufferedReader
- Read each line, make modifications to each line, and add it to a StringBuilder
- Once all the text has been read and modified, write the contents of the StringBuilder to a new file
- Replace the old file with the new file
This solution seems slightly "hacky" to me, especially if I have thousands of lines in my text file.
Anybody know of a better solution?