首页 > 解决方案 > FileNotFound 异常但文件存在

问题描述

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

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

标签: javafilenotfoundexception

解决方案


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

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

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

推荐阅读