Replacing/removing a line in a text file in java
public void removeLine() {
try {
File dir = new File("chars");
if(dir.exists()) {
String read;
File files[] = dir.listFiles();
for (int j = 0; j < files.length; j++) {
File loaded = files[j];
if (loaded.getName().endsWith(".txt")) {
Scanner s = new Scanner (loaded);
while (s.hasNextLine()) {
read = s.nextLine();
if (read.contains("char-15")) {
read.replace(read, "");
System.out.println(loaded.getName() +" -
Data: "+read);
break;
}
}
}
}
}
} catch (Exception e) {
}
}
What this should do is replace each line that contains "char-15", with an
empty String.
When I run this though, it doesn't delete the line in all the files. I
can't do this manually as there are well over 5000 files.
How can I make it delete this specific line in all of the files?
No comments:
Post a Comment