Remove a line from a text file



This example shows how to delete a line from a file.
The method removeLineFromFile() takes two parameters, the first parameter is the file to remove from and the second parameter is the content of the line to remove.
A temp file is created and written to, except for the content that matches the second parameter.
This way very large files can be handled without demanding so much internal memory.
The original file is then deleted and the temp file is renamed to the original filename.

For example, the file test.txt is located in the current directory and contains three lines, and we will remove the second line “bbbbb”.

The file test.txt now looks like this:







Search for more Java info on this site here:
Custom Search


Please type any questions here.