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

时间:2015-06-04 14:25:03

标签: java

我正在尝试扫描我的文本文档,以便在新行中on Thursday3 3之间打印所有行,但目前我只是在其中打印On Thursday。我该如何解决这个问题?

简单:

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

1 个答案:

答案 0 :(得分:2)

尝试这样的事情:

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