【问题标题】:How to read a single line from file in java如何在java中从文件中读取一行
【发布时间】:2013-12-17 21:12:33
【问题描述】:

如何从 java 中的文本文件中读取单行。 以及知道该行已完成的标准是什么。

其次

我读取文件,然后读取行函数并将其转换为字符串会跳过大量数据?应该是什么问题? 这是我的代码

String data = new String();
    while(infile.readLine() != null) {
    data = infile.readLine();
    System.out.println(data);
}

【问题讨论】:

    标签: java


    【解决方案1】:

    您正在读取额外的一行,因为第一个 readLine() 作为while 条件读取了一行,但它完全被使用。 while 循环内的第二个 readLine() 读取您分配给 data 的第二行并打印。

    因此您需要将while条件中读取的行分配给data并打印,因为这是第一行。

    while((data = infile.readLine()) != null) { // reads the first line
        // data = infile.readLine(); // Not needed, as it reads the second line
        System.out.println(data); // print the first line
    }
    

    此外,由于您只需要阅读第一行,因此您根本不需要 while。一个简单的if 就可以了。

    if((data = infile.readLine()) != null) { // reads the first line
        System.out.println(data); // print the first line
    }
    

    使用 BufferedReader 和您在 cmets 中发布的代码,您的 main 现在应该如下所示。

    public static void main(String[] args) {
        try {
            FileInputStream fstream = new FileInputStream(args[0]);
            BufferedReader infile = new BufferedReader(new InputStreamReader(
                    fstream));
            String data = new String();
            while ((data = infile.readLine()) != null) { // use if for reading just 1 line
                System.out.println(data);
            }
        } catch (IOException e) {
            // Error
        }
    }
    

    【讨论】:

    • 文件中的数据是 13 15 2 3 0 6 0 1 2 0 11 12 9 12 9 10 9 11 3 5 8 7 5 4 0 5 6 4 6 9 7 6 但控制台上的输出是 13 13 0 5 4 3 0 1 9 12 6 4 5 4 0 2 11 12 9 10 0 6 7 8 9 11 5 3 ????
    • read line() 必须转换为字符串,因为它还没有在字符串中。或者它给出类型转换的错误
    • 您使用哪个阅读器读取文件?我建议你使用BufferedReader
    • 我得到了你,我只使用了 readline() 一次,但它仍然会跳过许多值并且还会重复一些值。
    • 我再次详细发布了这个问题并附有输出。请看一下stackoverflow.com/questions/20324036/…
    【解决方案2】:

    如下修改你的代码

      while((data = infile.readLine()) != null) {  // read and store only line    
      System.out.println(data);
      }
    

    在你当前的代码中

       while(infile.readLine() != null) { // here you are reading one and only line
       data = infile.readLine(); // there is no more line to read
        System.out.println(data);
       }
    

    【讨论】:

    • 现在它将读取所有数据但给出异常它如何摆脱它 13 13 0 5 4 3 0 1 9 12 6 4 5 4 0 2 11 12 9 10 0 6 7 8 9 11 5 3 线程“main”中的异常 java.lang.NullPointerException at assignment.main(assignment.java:14)
    • @DaNyAlmAlIk 你的代码现在怎么样了?试试debug你的代码。
    【解决方案3】:

    第一件事:readLine() 仅返回字符串值,因此它不会转换为字符串。

    第二件事:在您的 while 循环中,您读取第一行并检查第一行的内容是否为空。但是当data = infile.readLine(); 执行时,它会从文件中取出第二行并打印到控制台。

    将您的 while 循环更改为:

    while((data = infile.readLine()) != null){ 
        System.out.println(data); 
    }
    

    如果你使用toString()方法,它会在尝试使用toString方法和nullinfile读取的内容时抛出NPE。

    【讨论】:

    • 现在它将读取所有数据但给出异常它如何摆脱它 13 13 0 5 4 3 0 1 9 12 6 4 5 4 0 2 11 12 9 10 0 6 7 8 9 11 5 3 线程“main”中的异常 java.lang.NullPointerException at assignment.main(assignment.java:14)
    • @DaNyAlmAlIk 您能否更新您在上述问题中的代码,以便我能清楚地理解问题
    • assignment.java 文件中的第 14 行是什么
    • 导入 java.util.Iterator;公共类分配 { public static void main(String[] args) { In infile = new In(args[0]); // 有向图 digraph = new Digraph(infile); // System.out.println(digraph.toString());字符串数据 = 新字符串();试试 { while((data = infile.readLine().toString()) != null){ System.out.println(data); } } catch (NullPointerException e) { } } } 这是完整的代码
    猜你喜欢
    • 2013-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-20
    相关资源
    最近更新 更多