0
我想编辑一个matlab文件并在某些特定行中替换某些编码部分的init。但是,使用下面的格式进行更改,它根本不会更改行上下文。 (它将打印相同的旧线)。任何想法我做错了什么? 'replaceAll'不适合用某些其他字词替换某些字词?在新的文本文件中打印被替换的行
在此先感谢。
try {
PrintWriter out = new PrintWriter(new FileWriter(filenew, true));
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.contains("stream.Values(strmatch('Test',stream.Components,'exact'))") {
String newline = line.replaceAll("stream.Values(strmatch('Test',stream.Components,'exact'))", "New Data");
out.println(newline);
System.out.println(newline);
} else {
out.write(line);
out.write("\n");
}
} // while loop
out.flush();
out.close();
scanner.close();
} catch (IOException e) {
e.printStackTrace();
}
噢谢谢。我不知道replace和replaceAll之间的区别。 – user3211165