[英]Replacing/removing a line in a text file
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. 这应该做的是用空字符串替换包含“ char-15”的每一行。
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.
我无法手动执行此操作,因为有5000多个文件。
How can I make it delete this specific line in all of the files? 如何使其删除所有文件中的此特定行?
You can do this easily by using Apache Common IO API
您可以使用
Apache Common IO API
轻松完成此操作
Here is full working example 这是完整的工作示例
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
public class FileUpdater {
public static void main(String[] args) throws IOException {
File dir = new File("D:\\dummy");
if (dir.exists() && dir.isDirectory()) {
File[] listFiles = dir.listFiles();
for (File file : listFiles) {
if (file.getName().contains(".txt")) {
String fileString = FileUtils.readFileToString(file);
String finalString = fileString.replaceAll("char-15", "");
FileUtils.writeStringToFile(file, finalString);
}
}
}
}
}
Above code will replace
char-15
to empty
in every file 上面的代码将把
char-15
replace
为每个文件为empty
} }
In Java 7 you can do something like this: 在Java 7中,您可以执行以下操作:
if (loaded.getName().endsWith(".txt")) {
Path path = Paths.get(loaded.getName());
String content = new String(Files.readAllBytes(path));
content = content.replaceAll("char-15", "");
Files.write(path, content.getBytes());
}
Java sting is immutable and final, whichever operation you made in String, it will create new instance only. Java字符串是不可变的,并且是最终的,无论您在String中进行哪种操作,它都只会创建新实例。 So
read.replace(read, "");
所以
read.replace(read, "");
is not help to replace word in your file. 无助于替换文件中的单词。
Firstly load file as String: 首先将文件加载为字符串:
public static String readAllText(InputStream is) {
StringBuilder sb = new StringBuilder();
try {
Reader r = new InputStreamReader(is);
int c;
while ((c = r.read()) != -1) sb.append(char.class.cast(c));
} catch (Exception e) {
//Handle Exception
}
return sb.toString();
}
then remove: 然后删除:
public static String removeUntil(String str, String c, int st)
{
StringBuilder sb = new StringBuilder(str);
str = sb.reverse().toString();
for(int i = 0;i<st;i++)
str = str.substring(0,str.lastIndexOf(c));
sb = new StringBuilder(str);
str = sb.reverse().toString();
return str;
}
for ex: removeUntil("I.want.remove.this.until.third.dot",".",3); 例如:removeUntil(“ I.want.remove.this.until.third.dot”,“。”,3); then result -> this.until.third.dot
然后结果-> this.until.third.dot
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.