【问题标题】:I'm working in netbeans trying to read data from a file and displaying it but it seems to be printing only some lines我在 netbeans 中工作,试图从文件中读取数据并显示它,但它似乎只打印了一些行
【发布时间】:2017-07-21 16:45:31
【问题描述】:

这里是代码

//import java.util.*;
package javaapplication8;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.*;

public class JavaApplication8 {

public static void main(String[] args) //throws Exception
{
    try{
        FileReader file = new FileReader("C:/Users/Rand/Desktop/fort.txt");
     BufferedReader reader = new BufferedReader(file);
     String abc;
     while(reader.readLine() != null)
     {
         abc = reader.readLine();
         System.out.println(abc);
     }
    }
    catch(Exception e)
            {
                   System.out.println( e);
            }

}
}

输入文件内容:

我想逃跑
让昨天的痛苦从这些裂缝中溜走...
躲起来

输出:

让昨天的痛苦从这些裂缝中溜走......

【问题讨论】:

    标签: java netbeans filereader


    【解决方案1】:

    您在每次迭代中调用 readLine 两次 - 将您的代码修改为:

    String abc;
    while((abc = reader.readLine()) != null) {
      System.out.println(abc);
    }
    

    或使用稍微不同的语法使abc 的范围更窄:

    for (String abc; (abc = reader.readLine()) != null; ) {
      System.out.println(abc);
    }
    

    【讨论】:

    • 我正要发这个,你抢先了
    • 问题还是答案?
    猜你喜欢
    • 2021-06-17
    • 2022-12-04
    • 2022-09-29
    • 1970-01-01
    • 2020-06-04
    • 1970-01-01
    • 1970-01-01
    • 2020-01-16
    • 1970-01-01
    相关资源
    最近更新 更多