gpt4 book ai didi

java - 写入新的文本文件

转载 作者:行者123 更新时间:2023-12-01 14:06:15 25 4
gpt4 key购买 nike

我读取一个文件并创建一个新文件,该文件复制其中的某些部分,删除一些行并将其替换为其他行。输入 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();
}

}
}

最佳答案

根据您在评论中提供的信息,应该是这样的。

  1. Its line.contains that shows when i want to edit and which line.

  2. That line that contains"" is the one I want to replace.

  3. And all ifs I include should be used and printed.

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");
}
}

关于java - 写入新的文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18879491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com