Java FileNotFound 异常错误,即使我指定了正确的路径

问题描述 投票:0回答:0

找不到文件,我不必指定文件位置,因为它与类 Test 在同一目录中。

代码:

File myFile = new File("aaa.txt");
    boolean isExisting = myFile.exists();
    System.out.println(isExisting); 
    

    try {
        
        FileReader f = new FileReader(myFile); 
        BufferedReader bf = new BufferedReader(f); 
        
        
        
        
        
      String line = bf.readLine();
      
      while(line != null) {
          
          System.out.println(line); 
          line = bf.readLine(); 
          
      }
      
      bf.close(); 
      
    } catch (FileNotFoundException e) {
        
        System.out.println("Can't find file");
        
        //e.printStackTrace(); 
        
        
    } catch (IOException e) {
        
        System.out.println("Error");
        
        
    }

错误(如果没有发现):

java.io.FileNotFoundException: aaa.txt (The specified file cannot be found)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:216)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157)
at java.base/java.io.FileReader.<init>(FileReader.java:75)
at Testing/Packageeee.Test.main(Test.java:29)

都在同一个目录下

java filenotfoundexception
© www.soinside.com 2019 - 2024. All rights reserved.