【发布时间】:2012-12-19 18:47:08
【问题描述】:
我有以下程序,文件“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;
}
}
【问题讨论】:
-
File infile = new File("C:\Users\john\workspace\Euler1\src\euler8.txt");给出绝对路径 -
或者把文件放到
C:\Users\john\workspace\Euler1\ -
检查该文件是否存在于类(或类似)文件夹(编译后存储类的位置)
-
@jlordo 做到了,但没用。我只会输入文字路径,但是我不知道为什么您的建议不起作用。
-
尝试了文字路径,但是由于路径包含 \ 它认为我正在尝试执行转义序列。使用 ' 而不是 " 也不起作用,并产生错误
invalid character constant
标签: java file-io filenotfoundexception