Modify a .txt file in Java -


I have a text file that I want to edit using Java. There are thousands of lines in it I basically want to iterate through the lines and change / edit / delete some text. It will often need to be frequent.

On the other sites I noticed that with the solutions, the general approach looks like this:

  • Open an existing file using a bufferder
  • Read, modify each line, and add it to stringbiller
  • Once all the text has been read and modified, write the contents of the stringbilder in a new file
  • New Change old file with file

Solution & amp; nbsp little "me" feel, especially if what I have thousands of lines in my text file.

Does anyone know about a better solution?

I have not done this recently in Java, but writing a whole file in memory is a bad idea. Sounds like

The best idea is that I can open a temporary file at a time in writing mode, and for each line, read it, modify it, if necessary, write in the temporary file. Finally, remove the original and rename the temporary file.

If you modify permissions on the file system, then you may also want to delete permissions and change the name.


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -