gpt4 book ai didi

java - 文件 I/O Java 程序无法识别 src 文件夹中的 .txt 文件

转载 作者:行者123 更新时间:2023-12-02 00:05:41 26 4
gpt4 key购买 nike

我有以下程序,文件“euler8.txt”存储在项目的 src 文件夹 C:\Users\john\workspace\Euler1\src\euler8.txt 中。当我尝试运行时,出现异常Exception in thread "main"java.io.FileNotFoundException: euler8.txt (The system Cannot find the file specified)

private static void euler8() throws IOException
{
int current;
int largest=0;
int c =0;
ArrayList<Integer> bar = new ArrayList<Integer>(0);
File infile = new File("C:/Users/xxxxxxxx/workspace/Euler1/euler8.txt");
BufferedReader reader = new BufferedReader(
new InputStreamReader(
new FileInputStream(infile),
Charset.forName("UTF-8")));
try
{
while((c = reader.read()) != -1)
{
bar.add(c);
}
}
finally{reader.close();}
for(int i=0; i<bar.size(); i++)
{
current = bar.get(i) * bar.get(i+1) * bar.get(i+2) * bar.get(i+3) * bar.get(i+4);
if(largest<current)
largest = current;
}
}

我现在看到的图片 http://img163.imageshack.us/img163/7017/halpbk.png

最佳答案

三种解决方案。选择一个:

1:将 euler.txt 从 src 目录上移
2:将行更改为

File infile = new File("./src/euler8.txt");

3:使用绝对路径

String path  = "C:/Users/john/workspace/Euler1/src/";
String file = "euler8.txt";
File infile = new File(path + file);

关于java - 文件 I/O Java 程序无法识别 src 文件夹中的 .txt 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13959028/

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