我读取一个文件并创建一个新文件,复制它的某些部分,删除一些行并将其替换为其他行。输入数组字符串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();
}
}
}
答案 0 :(得分:1)
这就是它假设您从评论中提供的信息中提取的内容。
它的line.contains显示我想要编辑的时间和哪一行。
包含“”的那一行是我要替换的那一行。
- 醇>
我应该使用和打印所有ifs。
String line;
while (scanner.hasNextLine()) {
// Get the line
line = scanner.nextLine();
// if line contains xxx then replace
if (line.contains("j(ac)")) {
line = line.replaceAll("/.*?/", "/"+raw1+"/");
} else if (line.contains("i(ac)")) {
line = line.replaceAll("/.*?/", "/"+raw2+"/");
} else if (line.contains("k(ac)")) {
line = line.replaceAll("/.*?/", "/"+raw3+"/");
}else if (line.contains("mp(k)")) {
line = line.replaceAll("/.*?/", "/"+raw4+"/");
}else if (line.contains("bp(k)")) {
line = line.replaceAll("/.*?/", "/"+raw5+"/");
}
// Write the line with replaced items
out.write(line);
out.write("\n");
}
现在您的代码在做什么:
while (scanner.hasNextLine()) {
// get the line (very first line) and store that in "line"
String line = scanner.nextLine();
// check if line contains "j(ac)"
if (line.contains("j(ac)")) {
// if "line" contains replace everything and save it to "newline"
String newline = line.replaceAll("/.*?/", "/"+raw1+"/");
// get next line getting used for nothing (second line stored nowhere)
scanner.nextLine();
// write the newline to output file.
out.write(newline);
out.write("\n");
}
// some more if else blocks executing same patterns explained above
else{
// if nothing contains in "line" then write to output file
out.write(line);
out.write("\n");
}
}