Java说FileNotFoundException但文件存在

Java说FileNotFoundException但文件存在,java,filenotfoundexception,Java,Filenotfoundexception,我有一个CS课程的作业,上面写着读一个有几个测试分数的文件,并要求我对它们求和平均。虽然求和和平均很容易,但我在读取文件时遇到了问题。老师说要用这个语法 Scanner scores = new Scanner(new File("scores.dat")); FileInputStream fi= new FileInputStream("D:/excelfiles/myxcel.xlsx"); 但是,这会抛出一个FileNotFoundException,但我已经反复

我有一个CS课程的作业,上面写着读一个有几个测试分数的文件,并要求我对它们求和平均。虽然求和和平均很容易,但我在读取文件时遇到了问题。老师说要用这个语法

Scanner scores = new Scanner(new File("scores.dat"));
FileInputStream fi= new FileInputStream("D:/excelfiles/myxcel.xlsx");
但是,这会抛出一个
FileNotFoundException
,但我已经反复检查了当前文件夹中是否存在该文件,之后,我认为它必须与权限有关。我为每个人更改了读写权限,但仍然不起作用,并且仍然抛出错误。有人知道为什么会发生这种情况吗

FileInputStream fi= new FileInputStream("D:/excelfiles/myxcel.xlsx");
编辑:它实际上指向一个目录,但是,我已经解决了这个问题。现在
file.exists()
返回
true
,但当我尝试将其放入
扫描仪时,它抛出
FileNotFoundException

FileInputStream fi= new FileInputStream("D:/excelfiles/myxcel.xlsx");
这是我所有的代码

import java.util.Scanner;
import java.io.*;
public class readInt{
        public static void main(String args[]){
                File file = new File("lines.txt");
                System.out.println(file.exists());
                Scanner scan = new Scanner(file);
        }
}
FileInputStream fi= new FileInputStream("D:/excelfiles/myxcel.xlsx");

代码本身工作正常。问题是,程序的工作路径指向的位置与您想象的不同

FileInputStream fi= new FileInputStream("D:/excelfiles/myxcel.xlsx");
使用此行并查看路径的位置:

System.out.println(new File(".").getAbsoluteFile());
FileInputStream fi= new FileInputStream("D:/excelfiles/myxcel.xlsx");

在许多情况下,运行时可能会抛出
FileNotFoundException

FileInputStream fi= new FileInputStream("D:/excelfiles/myxcel.xlsx");
  • 命名文件不存在。原因可能有很多,包括:

    FileInputStream fi= new FileInputStream("D:/excelfiles/myxcel.xlsx");
    
    • 路径名完全错误
    • 路径名看起来正确,但实际上是错误的,因为它包含您没有注意到的非打印字符(或同形符)
    • 路径名是相对的,相对于正在运行的应用程序的实际当前目录,路径名解析不正确。这通常是因为应用程序的当前目录不是您期望或假设的目录
    • 文件的路径已断开;e、 g.路径的目录名不正确,路径上的符号链接断开,或者某个路径组件存在权限问题
  • 命名文件实际上是一个目录

  • FileInputStream fi= new FileInputStream("D:/excelfiles/myxcel.xlsx");
    
  • 由于某些原因,无法打开命名文件进行读取
  • 好消息是,问题将不可避免地出现在上面。这只是一个确定哪一个的问题。以下是一些您可以尝试的东西:

    FileInputStream fi= new FileInputStream("D:/excelfiles/myxcel.xlsx");
    
    • 调用
      file.exists()
      将告诉您是否存在具有给定名称/路径名的文件系统对象
    • 调用
      file.isDirectory()
      将测试它是否为目录
    • 调用
      file.canRead()
      将测试该文件是否可读
    • 此行将告诉您当前目录是什么:

      System.out.println(new File(".").getAbsolutePath());
      
      FileInputStream fi= new FileInputStream("D:/excelfiles/myxcel.xlsx");
      
    • 此行将打印出路径名,以便更容易发现意外的前导或训练空白:

      System.out.println("The path is '" + path + "'");
      
      FileInputStream fi= new FileInputStream("D:/excelfiles/myxcel.xlsx");
      
      在输出中查找意外的空格、换行符等

    • FileInputStream fi= new FileInputStream("D:/excelfiles/myxcel.xlsx");
      

    事实证明,您的示例代码存在编译错误

    FileInputStream fi= new FileInputStream("D:/excelfiles/myxcel.xlsx");
    
    我运行您的代码时没有考虑Netbeans的投诉,结果得到以下异常消息:

    FileInputStream fi= new FileInputStream("D:/excelfiles/myxcel.xlsx");
    
    线程“main”java.lang.RuntimeException中的异常:不可编译 源代码-未报告的异常java.io.FileNotFoundException;必须 被抓住或宣布被扔掉

    FileInputStream fi= new FileInputStream("D:/excelfiles/myxcel.xlsx");
    
    如果您将代码更改为以下内容,它将修复该问题

    FileInputStream fi= new FileInputStream("D:/excelfiles/myxcel.xlsx");
    

    说明:
    Scanner(File)
    构造函数被声明为引发
    FileNotFoundException
    异常。(扫描仪可能无法打开文件。)现在
    FileNotFoundException
    是选中的异常。这意味着可能引发异常的方法必须捕获异常或在
    throws
    子句中声明它。上述修复采用后一种方法。

    显然有许多可能的原因,前面的答案很好地记录了这些原因,但以下是我在一个特定案例中解决这些问题的方法:

    FileInputStream fi= new FileInputStream("D:/excelfiles/myxcel.xlsx");
    
    我的一个学生有这个问题,我为了想办法差点把头发扯掉。结果证明,该文件不存在,尽管看起来确实存在。问题在于Windows 7被配置为“隐藏已知文件类型的文件扩展名”。这意味着,如果文件名为“data.txt”,则其实际文件名为“data.txt.txt”

    FileInputStream fi= new FileInputStream("D:/excelfiles/myxcel.xlsx");
    

    希望这能帮助其他人节省一些时间。

    我最近发现了一个有趣的案例,当磁盘上明显存在文件时,会生成FileNotFoundException。
    FileInputStream fi= new FileInputStream("D:/excelfiles/myxcel.xlsx");
    
    在我的程序中,我从另一个文本文件读取文件路径并创建文件对象:

    //String path was read from file
    System.out.println(path); //file with exactly same visible path exists on disk
    File file = new File(path); 
    System.out.println(file.exists());  //false
    System.out.println(file.canRead());  //false
    FileInputStream fis = new FileInputStream(file);  // FileNotFoundExeption 
    
    FileInputStream fi= new FileInputStream("D:/excelfiles/myxcel.xlsx");
    
    问题的原因是路径末尾包含不可见的
    \r\n
    字符

    FileInputStream fi= new FileInputStream("D:/excelfiles/myxcel.xlsx");
    
    对我来说,解决办法是:

    File file = new File(path.trim()); 
    
    FileInputStream fi= new FileInputStream("D:/excelfiles/myxcel.xlsx");
    
    概括来说,不可见/非打印字符可能包括空格或制表符,也可能包括其他字符,它们可能出现在路径的开头、结尾或嵌入到路径中。修剪在某些情况下有效,但并非所有情况下都有效。有几件事可以帮助您发现此类问题:

    FileInputStream fi= new FileInputStream("D:/excelfiles/myxcel.xlsx");
    
  • 输出带有引号字符的路径名;e、 g

      System.out.println("Check me! '" + path + "'");
    
    FileInputStream fi= new FileInputStream("D:/excelfiles/myxcel.xlsx");
    
    仔细检查输出中不应该出现的空格和换行符

  • FileInputStream fi= new FileInputStream("D:/excelfiles/myxcel.xlsx");
    
  • 使用Java调试器逐个字符仔细检查路径名字符串,查找不应该存在的字符。(同时检查同形文字字符!)

  • FileInputStream fi= new FileInputStream("D:/excelfiles/myxcel.xlsx");
    

    除了这里提到的所有其他答案,你可以做一件对我有用的事情

    FileInputStream fi= new FileInputStream("D:/excelfiles/myxcel.xlsx");
    
    如果您是通过扫描仪或命令行参数读取路径,而不是直接从Windows资源管理器复制粘贴路径,只需手动键入路径。

    FileInputStream fi= new FileInputStream("D:/excelfiles/myxcel.xlsx");
    

    它对我有用,希望它能帮助别人:)

    根据文件的权限属性,操作系统可以阻止文件的读写

    FileInputStream fi= new FileInputStream("D:/excelfiles/myxcel.xlsx");
    
    如果您正试图从文件中读取,那么我建议使用文件的setReadable方法将其设置为true,或者此代码