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

标签 java file-io filenotfoundexception

我有以下程序,文件“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/

相关文章:

java - 发生错误时无限循环

java - 如何在java中以UTF-8编码保存文本文件?

java - scala.io.Source 找不到现有文件

java - log4j:错误 setFile(null,true)调用失败。java.io.FileNotFoundException:

java - Java 中存储的对象的条目数在哪里

java - 尝试从从 sqlite 中提取的对象列表中调用方法时的空对象引用

java - OOP:抽象类可以/应该调用它自己的抽象方法吗?

java - 随着写入操作数量的增加,文件写入速度急剧下降

java - JFileChooser 和 Bufferedreader 遇到问题

c# - 在 Crystal Report 中分配 DataSource 时发生 FileNotFoundException