gpt4 book ai didi

java - 在新文件中打印文本文档的特定行

转载 作者:行者123 更新时间:2023-12-02 04:36:48 26 4
gpt4 key购买 nike

我正在尝试扫描我的文本文档,以在新文档中打印 周四3 3 之间的所有行,但目前我只得到 周四 印在其中。我该如何解决这个问题?

简单:

U.S. stocks dipped at
the open
on Thursday
after
a persistent
selloff in the
global bond
markets
overshadowed
slightly better-than-expected
U.S. jobless
3 3
The Dow Jones industrial
claims data

代码:

File file = new File("D:\\hl_sv\\L03MF.txt");

try (PrintWriter writer = new PrintWriter("D:\\hl_sv\\L03MF2.txt");

Scanner scanner = new Scanner(file)) {

while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String outputLine = "";

if (line.startsWith("Dez im ServiceCenter am ZOB)")
&& line != "3 3") {
outputLine = line;
writer.println(outputLine);

}


}
} catch (Exception e) {
e.printStackTrace();
}

最佳答案

尝试这样的事情:

boolean shouldPrint = false;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();

if (line.startsWith("on Thursday)") {
shouldPrint = true;
} else if (line.startsWith("3 3") {
shouldPrint = false;
}

if(shouldPrint) writer.write(line);
}

关于java - 在新文件中打印文本文档的特定行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30646532/

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