【问题标题】:Java Filenotfound exception being thrown抛出 Java Filenotfound 异常
【发布时间】:2012-12-19 22:30:40
【问题描述】:

即使我的文件位于我所说的确切目录中,也会为我的代码引发 FileNotFound 异常。我也尝试过...new File("euler8.txt");...,但没有成功。我的代码如下:

        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

【问题讨论】:

  • 您是否尝试过打印文件的绝对路径,并可能将该输出与 new File(".") 的绝对路径进行比较,看看您是否在路径中犯了任何错误?文件名大小写是否正确?
  • 尝试在声明infile 后立即添加System.out.println(infile.exists());。如果返回错误,则您的文件路径有问题 - 继续返回目录,直到您得到正确。如果它回来是真的...... ???
  • 尝试使用../Euler1/euler8.txt作为你的文件路径。
  • ../Euler1/euler8.txt 不起作用。同样的错误

标签: java io filenotfoundexception


【解决方案1】:

除了建议的所有其他内容外,您可以检查您是否遇到此问题(我们已经在我们的实验室中看到):文件具有两倍的扩展名。换句话说,请确保您的 euler8.txt 确实被称为而不是 euler8.txt.txt,例如,因为使用隐藏扩展名,文件资源管理器将显示第一个,但如果您不这样做,它最初可能不会让您感到奇怪不记得它应该隐藏扩展名。

【讨论】:

  • 从@James Roberts 给出的图片来看,我认为你是对的。文件名显示“euler8.txt”,但它应该只显示“euler8”,因为扩展名在 Windows 7 中单独指定。
  • @JavaNewbie_M107 我没有注意到这张照片。好像就是这样。让我们希望 OP 回来看看。
  • 为了帮助... 在我的开发 Windows 机器上,我关闭了该选项,因为这个问题让我经常使用在 Windows 中重命名文件的简单方法,但扩展名缺乏可见性。
  • @TheodorosChatzigiannakis 获胜者
【解决方案2】:

正斜杠可以正常工作,并且是首选,因为它们可以在任何平台上工作(相对路径优于绝对路径)。确保您的路径按指定存在,并验证您对指向该文件的目录具有读取权限。例如,如果您以其他用户身份运行 java 程序,您可能没有“myuser”文件夹的读取权限。

【讨论】:

    【解决方案3】:

    如果所有目录都不存在,则此代码将不起作用,因此我假设(希望我是正确的)您有错字或缺少文件夹。

    我通常更喜欢有一个java.io.File 对父目录的引用,然后在后续文件引用中将其用作父目录,即:

    File dir = new File("parentDir");
    File inFile = new File(dir, "fileName");
    

    另外,java.io.File 有一个 exists() 方法返回 true 或 false,其后续的 mkdir()mkdirs()createNewFile() 如果它们实际创建了请求的文件,则返回 true 或 false。

    也就是说,我将您的代码修改为以下内容,并在我的机器上执行;但我不知道你试图通过这个运行什么数据。

        int current;
        int largest = 0;
        int c = 0;
        ArrayList<Integer> bar = new ArrayList<Integer>(0);
        File dir = new File("C:/Users/myuser/workspace/Euler1");
        if(!dir.exists()){
            dir.mkdirs();
        }
        File infile = new File(dir, "euler8.txt");
        if(!infile.exists()){
            infile.createNewFile();
        }
        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;
            }
        }
    

    【讨论】:

      【解决方案4】:

      反斜杠是完全没有必要的。我运行了这个程序:

      import java.io.BufferedReader;
      import java.io.File;
      import java.io.FileInputStream;
      import java.io.IOException;
      import java.io.InputStreamReader;
      import java.nio.charset.Charset;
      
      public class Test {
        public static void main(String[] args) throws IOException {
          File infile = new File("C:/Users/pats/workspace/test/euler8.txt");
          BufferedReader reader = new BufferedReader(new InputStreamReader(
              new FileInputStream(infile), Charset.forName("UTF-8")));
          try {
            String s;
            while ((s = reader.readLine()) != null) {
              System.out.println(s);
            }
          } finally {
            reader.close();
          }
        }
      }
      

      非常相似,它打印了我文件的内容。

      我认为您需要检查该文件是否存在以及您是否可以访问它。

      【讨论】:

        猜你喜欢
        • 2022-06-30
        • 1970-01-01
        • 2010-10-30
        • 2012-07-23
        • 2011-11-10
        • 1970-01-01
        • 2012-05-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多