【发布时间】:2014-06-19 09:42:55
【问题描述】:
我正在尝试编辑 matlabfile 并在某些特定行中替换一些编码部分 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();
}
【问题讨论】:
标签: java java.util.scanner printwriter replaceall