【问题标题】:Java - unable to read from file with this codeJava - 无法使用此代码从文件中读取
【发布时间】:2017-02-26 15:47:58
【问题描述】:
public class RandomFuncs {

public static void main(String[] args) {
    readingFile();
}
public static void readingFile() {
    String file = "coins.txt";
    try {
        FileReader fileReader = new FileReader(file);
        BufferedReader bufferedReader = new BufferedReader(fileReader);

        String line = bufferedReader.readLine();

        //System.out.println(line);

        while (line != null){ System.out.println(line);}
    }
    catch (IOException ex){
        System.out.println("Unable to read to file " + file + ".Due to " + ex);
    }
}

}

变量行总是返回为空,我不知道为什么。文件中有文本行,但没有被读取。

【问题讨论】:

    标签: file io


    【解决方案1】:

    尝试将绝对文件路径放入“文件”变量中。

    还有

    String line = bufferedReader.readLine();
    
    while (line != null){ System.out.println(line);}
    

    不起作用,它会打印“line”,直到该特定字符串变为 null,而这永远不会。脚本只是永远打印文件中的第一行。

    【讨论】:

      猜你喜欢
      • 2020-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-31
      • 1970-01-01
      • 1970-01-01
      • 2013-02-20
      • 1970-01-01
      相关资源
      最近更新 更多