【问题标题】:File I/O Java Program not recognizing my .txt file in the src folder文件 I/O Java 程序无法识别 src 文件夹中的 .txt 文件
【发布时间】: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;
    }
}

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

【问题讨论】:

  • 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


【解决方案1】:

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

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);

【讨论】:

  • 奇怪,解决方案 3 必须一直有效。你确定文件存在吗?由于 Windows,C:/Users 也可能很关键。打开命令行窗口并切换到该目录,并查看路径是否具有正确的名称。
【解决方案2】:

您应该将文本文件移出src 文件夹,将其直接放在项目文件夹下。


或者,对于您当前的位置,将路径更改为:-

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

您提供的路径是相对于根文件夹的,因此如果您提供"euler8.txt",它将无法找到该文件。您需要提供相对于项目文件夹的路径才能进入 src 文件夹。

您也可以提供absolute path,但这不是要走的路,因为每次将项目移动到不同的位置时都会要求修改路径。

【讨论】:

    【解决方案3】:

    这一行在根文件夹中寻找您的文本文件。

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

    你需要像这样给出绝对路径

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

    或者 jLordo 建议将您的文件移动到您的根文件夹

    【讨论】:

    • 您需要在路径中提供forward-slash。或者,双反斜杠。
    【解决方案4】:

    试试这个

      InputStream inputStream = getClass().getResourceAsStream("euler8.txt");
            String sa = "";
            int cc;
            while((cc = inputStream.read()) != -1) {
                sa += (char) cc;
            }
    

    假设 euler8.txt 在 src 文件夹中

    【讨论】:

      【解决方案5】:

      正如所指出的,当前工作目录是启动程序的目录,而不是 src 目录。您始终可以使用

      找到当前工作目录
      String workingDir = new java.io.File( "." ).getCanonicalPath();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-02-27
        • 2018-04-27
        • 1970-01-01
        • 2011-08-26
        • 2017-01-19
        • 1970-01-01
        • 2021-06-18
        • 1970-01-01
        相关资源
        最近更新 更多