How can I delete a specific string in a text file?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Locate the file.
Create a temporary file (otherwise you’ve to read everything into Java’s memory first).
Determine the charset.
Determine the string you’d like to delete.
Open the file for reading.
Open the temp file for writing.
Read the file line by line.
Delete the string from the line.
Write it to temp file.
Close the reader and writer (preferably in the
finallyblock).Delete the file.
Rename the temp file.
See also: