我读取一个文件并创建一个新文件,该文件复制其中的某些部分,删除一些行并用其他行替换它们。输入数组字符串 raw 的类型为 [Aaa,Bbb,Ccc,..],用于替换部分行。在新文件中,未编辑的部分正确打印,但已编辑的部分以这种方式打印。第一个打印,第二个不打印,第三个是,第四个没有,第五个是......看起来当我编辑一行时,我也删除了下面的一行。我尝试删除 out.write("\n") 或scanner.nextLine() ,但它也没有工作。任何想法我可以尝试什么?提前致谢
For example:
OLD TEXT:
.....
LINE 6 / contains j(ac)
LINE 7 / contains i(ac)
LINE 8 / contains k(ac)
LINE 9 / contains mp(ac)
LINE 10 /contains bp(ac)
.....
NEW TEXT (NEW FILE):
.....
LINE NEW6
LINE NEW7
LINE NEW8
LINE NEW9
LINE NEW10
.....
public static void main(String[] args) {
new class();
class.query();
File file = new File("file");
File filenew = new File("file");
try {
PrintWriter out = new PrintWriter(new FileWriter(filenew, true));
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.contains("j(ac)")) {
String newline = line.replaceAll("/.*?/", "/"+raw1+"/");
scanner.nextLine();
out.write(newline);
out.write("\n");
} else if (line.contains("i(ac)")) {
String newline = line.replaceAll("/.*?/", "/"+raw2+"/");
scanner.nextLine();
out.write(newline);
out.write("\n");
} else if (line.contains("k(ac)")) {
String newline = line.replaceAll("/.*?/", "/"+raw3+"/");
scanner.nextLine();
out.write(newline);
out.write("\n");
}else if (line.contains("mp(k)")) {
String newline = line.replaceAll("/.*?/", "/"+raw4+"/");
scanner.nextLine();
out.write(newline);
out.write("\n");
}else if (line.contains("bp(k)")) {
String newline = line.replaceAll("/.*?/", "/"+raw5+"/");
scanner.nextLine();
out.write(newline);
out.write("\n");
} else{
out.write(line);
out.write("\n");
}
}
out.flush();
out.close();
scanner.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}