gpt4 book ai didi

使用 File 对象的 Java.io.FileNotFoundException

转载 作者:行者123 更新时间:2023-12-04 05:10:41 25 4
gpt4 key购买 nike

File read = new File("Numbers.txt");
Scanner inputFile = new Scanner(read);

while(inputFile.hasNext())
{
sum = inputFile.nextDouble() + sum;
count++;
}

inputFile.close();//close the input file

我正在尝试从文本文件中读取数据 Numbers.txt下面的代码编译得很好,但我得到了 Java.io.FileNotFoundException程序运行时出错。我也试过输入完整的文件路径,但我可能做错了。有什么想法吗?

最佳答案

确保您的文本文件在您的 java 文件所在的文件夹中
因为你使用了直接路径。
并尝试此代码检查,如果仍然无法正常工作。

BufferedReader read = new BufferedReader(new FileReader("yourTextFile.txt"));
String line = read.readLine();

while(line !=null)
{
System.out.println(line);
line=read.readLine();
}
}catch(Exception ex)
{System.out.println(ex.getMessage());}

关于使用 File 对象的 Java.io.FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14966115/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com