从扫描仪读取文件但无法打印

Reading a file from Scanner but it won't print

我有一个 class 可以读取 txt 文件的一部分。该代码适用于某些人,但对我来说它不会打印到控制台或附加到我的 JTextArea(我的最终目标)

我只是想知道问题出在代码还是文件上。

 public void readFiles(String fileString)
        throws FileNotFoundException {

    file = new File(fileString);
    Scanner scanner = null;
    String line = "";

    // access file
    try {
        scanner = new Scanner(file);
    } catch (FileNotFoundException e) {
        System.out.println("File not found.");
        return; // don't continue if the file is not found
    }

    // if more lines in file, go to next line
    Boolean started = false;
    while (scanner.hasNext()) {

        line = scanner.nextLine();
        if (line.equals("BGEND")) {
            started = false;

        }

        if (started) // tag in the txt to locate position
        {

            System.out.println(line);//won't print on my console
            lb1.setText(line); //attaches to a JTextArea.
            window2.add(lb1); //adds to JPanel
        }

        if (line.equals("BGSTART")) {
            started = true;
        }
    }
    scanner.close();
} 

这是我的文件

BGSTART   
Ashley the principal at Leicester,
Memorial School has been given the
task of matching some students names
to their bus numbers and departure
time, after their computer system went 
down. Using only the clues that follow, 
match each student to their bus number 
and route to determine who goes where 
and when!
Remember, as with all grid-based logic 
puzzles, no option in any category will 
ever be used more than once. If you get 
stuck or run into a problem try the Clear 
button to remove any mistakes that might 
be present on the grid, or use one of your 
4 hints with the Hint button to see what is 
the next logical step to solve the puzzle.
BGEND
        while (scanner.hasNext()) {
        line = scanner.nextLine();
        if (line.equals("BGSTART")) {
            started = true;
        }

        if (line.equals("BGEND")) {
            started = false;

        }
        if (started) // tag in the txt to locate position
        {

            System.out.println(line);//won't print on my console
            lb1.setText(line); //attaches to a JTextArea.
            window2.add(lb1); //adds to JPanel
        }

    }

这样程序首先检查第一行是否等于 "BGSTART" 字符串,将布尔值设置为 true 并继续下一个 if 语句并执行该代码,只要它到达末尾文件。

Boolean started = false;
while (scanner.hasNext()) {

    line = scanner.nextLine();
    if (line.startsWith("BGSTART")) {
        started = true;
        continue;
    }

    if (line.startsWith("BGEND")) {
        started = false;
        break;
    }

    if (started) // tag in the txt to locate position
    {
        System.out.println(line);//won't print on my console
        lb1.setText(line); //attaches to a JTextArea.
        window2.add(lb1); //adds to JPanel
    }
}
scanner.close();

试试这个。基本上,如果你有类似的东西:

some lines of text here
BGSTART
some lines of text here
BGEND

代码应跳过 BGSTART 标记之前的文本行。一旦遇到此标记,布尔标志值将更改为 TRUE,让代码知道您开始阅读有效行,并立即跳过其余代码以获取下一行。如果遇到 BGEND 标记,则表示结束。所以在这种情况下,您想完全跳出循环。然后,如果布尔标志设置为 TRUE,您想要对从文件中读取的文本行执行某些操作。

我认为上面的代码正是您所需要的。它也应该适用于这样的情况:

some lines of text here
BGSTART
some lines of text here
BGEND
some lines of text here
BGSTART
some lines of text here
BGEND

有效地只读取 BGSTART 和 BGEND 之间的文本行,而不是 BGEND 和 BGSTART 之间的文本行。