我看过很多关于此的帖子,但我无法做到这一点。我需要做这样的事情..可以说,我有两个文件 a.txt,b.txt。我应该在 a.txt 中搜索一个字符串/行并将其替换为 b.txt 的内容。我认为它只是几行简单的代码。我尝试了下面的代码,但它不工作......
File func = new File("a.txt");
BufferedReader br = new BufferedReader(new FileReader(func));
String line;
while ((line = br.readLine()) != null) {
if (line.matches("line to replace")) {
br = new BufferedReader(
new FileReader(func));
StringBuffer whole = new StringBuffer();
while ((line = br.readLine()) != null) {
whole.append(line.toString() + "\r\n");
}
whole.toString().replace("line to replace",
"b.txt content");
br.close();
FileWriter writer = new FileWriter(func);
writer.write(whole.toString());
writer.close();
break;
}
}
br.close();
请帮忙 !