【问题标题】:FileNotFound Exception but the file exists [duplicate]FileNotFound 异常但文件存在[重复]
【发布时间】:2018-06-21 15:24:25
【问题描述】:

我尝试使用 java.io.File 类读取文件,但它正在抛出 filenotfound exception。但是文件在那里,当我使用exists 方法时,它返回false

public static void main(String[]args){
    File file = new File("q.txt");
    Scanner fileScanner = new Scanner(file);
}

【问题讨论】:

  • 在哪里?在您执行程序的目录中?
  • 这个文件在哪里?你试过写完整的地址吗?
  • 还需要声明文件路径
  • 好像是相对路径问题...请尝试给出完整路径并访问
  • File file = new File("q.txt"); 更改为 File file = new File("q.txt"); System.out.println(file.getAbsolutePath()); 以显示 JVM 实际认为该文件的位置(并开始调试此文件)..

标签: java filenotfoundexception


【解决方案1】:

请在File构造函数中提供完整路径

public static void main(String[]args){
    File file = new File("Please provide complete path here");
    Scanner fileScanner = new Scanner(file);
}

【讨论】:

    【解决方案2】:

    确保文件在项目文件夹下,而不是在包文件夹中。

    【讨论】:

      【解决方案3】:

      您正在尝试通过提供文件名来提供文件的相对路径。

      请提供文件的绝对路径。例如

      File f = new File("C:\\Hello\\AnotherFolder\\The File Name.PDF");  
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-08-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多