[英]Writing in a new text file
我读取了一个文件并创建了一个新文件,该文件将复制其中的一部分,删除一些行,并用其他行替换。 输入的arraystring 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();
}
}
}
这就是从注释中提供的信息中得出的结论。
它的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");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.