更新时间:2023-01-15 18:19:22
一种方法是使用 String.replaceAll()
:
File log= new File("log.txt");
String search = "textFiles/a\\.txt"; // <- changed to work with String.replaceAll()
String replacement = "something/bob.txt";
//file reading
FileReader fr = new FileReader(log);
String s;
try {
BufferedReader br = new BufferedReader(fr);
while ((s = br.readLine()) != null) {
s.replaceAll(search, replacement);
// do something with the resulting line
}
}
您还可以使用正则表达式或 String.indexOf()
来查找搜索字符串在一行中出现的位置.
You could also use regular expressions, or String.indexOf()
to find where in a line your search string appears.