检查该行是否是文本文档中的最后一行

时间:2015-06-08 16:53:12

标签: java

我正在尝试检查文本文档中的最后一行是否在文本文档中写了一行。

我在下面尝试了以下代码,但我收到此错误:

  

线程中的异常" main" java.util.NoSuchElementException:找不到行

我在hello.txt文档中有文字,所以我不明白这个错误意味着什么。

我该如何解决?

我感谢任何帮助。

代码:

public static void main(String[] args) {
    try (PrintWriter writer = new PrintWriter("D:\\hl_sv\\hello2.txt");

    Scanner scanner = new Scanner("D:\\hl_sv\\hello.txt")) {

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

            // In the case the line is the last one and there is no number.
            // The content of the arrayList must be printed.
            if ((line = scanner.nextLine()).isEmpty()) {
                System.out.println("last line in the text document");
                writer.write("last line in the text document.");
            }

        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

修改

public static void main(String[] args) {
    try (PrintWriter writer = new PrintWriter("D:\\hl_sv\\hello2.txt");

            Scanner scanner = new Scanner("D:\\hl_sv\\hello.txt")) {
                while (scanner.hasNextLine()) {
                    String line = scanner.nextLine();

                    // In the case the line is the last one and there is no number.
                    // The content of the arrayList must be printed.
                    if (line.isEmpty()) { // <-- Removed  = scanner.nextLine()
                        System.out.println("last line in the text document");
                        writer.write("last line in the text document.");
                    }else{
                        writer.write(line);
                    }

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

使用编辑代码我不再收到错误,但我在文本文档D:\hl_sv\hello.txt中打印了此hello2.txt行?

5 个答案:

答案 0 :(得分:1)

<强> 更新

我在我的电脑上试用这个代码,并且根据你的规范工作正常。

这是我的代码:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

class Test {

    public static void main(String[] args) {
        try {

            PrintWriter writer = new PrintWriter(new File(
                    "../Test/src/com/hello2.txt"));

            Scanner scanner = new Scanner(new File("../Test/src/com/hello.txt"));
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine()+"\n";
                System.out.println(line);
                writer.write(line);
            }

            System.out.println("last line in the text document");
            writer.write("last line in the text document.");
            scanner.close();
            writer.close();
        } catch (FileNotFoundException e) {
            System.out.println(e);
        }
    }
}

<强>被修改

您应该添加new File

new PrintWriter(new File("D:\\hl_sv\\hello2.txt"));

new Scanner(new File("D:\\hl_sv\\hello.txt"))); 

因为PritWriter需要File作为参数,Scanner需要StringFile作为参数。由于您在String中仅使用Scanner ( "D:\\hl_sv\\hello.txt" ),因此您在hello2.txt文件中获得了“D:\ hl_sv \ hello.txt”行。

您可以使用在main函数之后立即声明的line变量来访问主类中的line变量。

所以你的代码应该是:

public static void main(String[] args) {
    String line="";

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

    Scanner scanner = new Scanner(new File("D:\\hl_sv\\hello.txt"))) {

        while (scanner.hasNextLine()) {
            line = scanner.nextLine();

            // In the case the line is the last one and there is no number.
            // The content of the arrayList must be printed.

        }

        System.out.println("last line in the text document");
        writer.write("last line in the text document.");
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

您可以非常简单的方式检查该文本文件的最后一行/逻辑只需在while循环后执行操作。因为在你的代码循环之后,你的文本文件中没有行。

public static void main(String[] args) {
    try (PrintWriter writer = new PrintWriter(new File("D:\\hl_sv\\hello2.txt"));

    Scanner scanner = new Scanner(new File("D:\\hl_sv\\hello.txt"))) {

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

            // In the case the line is the last one and there is no number.
            // The content of the arrayList must be printed.

        }

        System.out.println("last line in the text document");
        writer.write("last line in the text document.");
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

使用此声明时

if ((line = scanner.nextLine()).isEmpty())

scanner找不到任何行(在文件结尾之后),因此没有这样的元素(下一行)存在,所以你得到了这个异常

  

线程“main”中的异常java.util.NoSuchElementException:找不到行

答案 1 :(得分:0)

请参阅链接的帖子了解更多信息。

How to determine when end of file has been reached?

基本上,hasNextLine()函数会告诉您是否已到达文件末尾(您似乎已经使用过)。如果你想把它作为一个特征,我会说稍微重新排列你的循环以包含它。

答案 2 :(得分:0)

您似乎正在执行以下

line = scanner.nextLine();

每次循环两次。

鉴于这些信息,删除其中一个电话将解决您的问题。

答案 3 :(得分:0)

您的问题是,您每次都要移动扫描仪两行,因为您拨打了.nextLine()两次。

修正:

public static void main(String[] args) {
    try (PrintWriter writer = new PrintWriter("D:\\hl_sv\\hello2.txt");

    Scanner scanner = new Scanner("D:\\hl_sv\\hello.txt")) {

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

            // In the case the line is the last one and there is no number.
            // The content of the arrayList must be printed.
            if (line.isEmpty()) { // <-- Removed  = scanner.nextLine()
                System.out.println("last line in the text document");
                writer.write("last line in the text document.");
            }

        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

答案 4 :(得分:0)

只需测试hasNextLine()

        if ( ! scanner.hasNextLine() ) {
            System.out.println("last line in the text document");
            writer.write("last line in the text document.");
        }

一个相关的,相当有趣的话题是,如果我们使用Files.lines(path),这是Stream<String>,我们如何检测最后一行:)