0
matlabfile을 편집하고 일부 특정 행에서 일부 코딩 부분 init을 바꾸려고합니다. 그러나 아래의 형식을 사용하여 변경 사항을 적용하면 라인 컨텍스트가 전혀 변경되지 않습니다. (동일한 이전 행을 인쇄합니다). 어떤 생각인지 내가 뭘 잘못 했니? 'replaceAll'은 일부 단어를 줄의 다른 단어로 바꾸는 데 적합하지 않습니까?새 텍스트 파일에서 대체 된 행 인쇄
미리 감사드립니다. String
에
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