【问题标题】:I keep getting "FileNotFoundException" even though I handled the exception and the file does exist.即使我处理了异常并且文件确实存在,我仍然收到“FileNotFoundException”。
【发布时间】:2013-04-13 18:38:25
【问题描述】:
Scanner read=new Scanner(file);

此语句中出现“FileNotFoundException”异常。

  import java.util.*;
  import java.io.*;

  public class shoppingList {

        public static void main(String []args)throws IOException {

              File file=new File("MyList.txt");

              try {

                   if(file.exists()==false)
                    throw new FileNotFoundException("the file input doesn't exist");
              }
              catch(FileNotFoundException e){System.out.print(e.getMessage());}

              //I tried handling the exception but it didn't work

              Scanner read=new Scanner(file);


              File outfile=new File("MyReceipt.txt");
              FileOutputStream fos=new FileOutputStream(outfile);
              PrintWriter output=new PrintWriter(fos);

              while(read.hasNext()) {
                 String item=read.next();
                 double price=read.nextDouble();
                 String status=read.next();

                 output.println("My Receipt: ");
                 output.println("--------------------");

                 if(status.equals("Done")==true)
                    output.println(item+"  "+price);


                 double total=0;


                 total+=price;
                 output.println("--------------------");
                 output.println("total= "+total);
             }

             read.close();
             output.close();
      }
 }

【问题讨论】:

  • 好吧,您运行程序的地方不存在该文件。这就是你得到错误的原因。为了确保您的文件所在的路径,请注释 main 方法中的所有代码并添加此 new File("something.txt").createNewFile();,运行它,您将看到这个新文件已创建(以及您应该放置 MyList.txt 文件)。
  • @LuiggiMendoza - 这是个好主意。或者,OP 应该尝试System.out.println(new File("MyList.txt").getAbsolutePath()); 来调试程序在哪里寻找文件。
  • @sgp15 如果 MyList.txt 文件不存在,它将不起作用...
  • @LuiggiMendoza - File 不需要存在实际文件。它只是一个抽象,不像 FileInputStreamFileOutStream 需要文件存在。我也测试过。

标签: java file exception exception-handling


【解决方案1】:

问题是程序只是在 catch 语句之后继续。因此,即使您处理了file.exists 抛出的第一个 FileNotFoundException,您也会得到Scanner read=new Scanner(file); 引发的第二个,而这个没有得到处理。

【讨论】:

    【解决方案2】:

    new File("MyList.txt") 将尝试在当前目录中查找文件。

    当前目录取决于程序运行的环境。例如,如果程序在Eclipse IDE中运行,则Java项目为当前目录。

    尝试提供绝对路径。例如。 C:\\workspace\\project\\MyList.txt.

    或者,将文件放在源代码树或包下,并通过类路径打开InputStream。例如,如果它放在一个名为my.foo.shopping 的包中,您可以直接创建一个Scanner,如下所示。

    Scanner read=new Scanner(shoppingList.class.getResourceAsStream("/my/foo/shoppin/MyList.txt"));
    

    【讨论】:

      【解决方案3】:

      在您捕获FileNotFoundException 后,您尝试读取另一个文件并抛出另一个FileNotFoundException

      问题出在一行:

       Scanner read=new Scanner(file);
      

      解决方法是将所有代码放在try块中:

      File file=new File("MyList.txt");
      
      try{
      
          if(file.exists()==false)
               throw new FileNotFoundException("the file input doesn't exist");
      
             //i tried handling the exception but it didn't work
      
               Scanner read;
                  read = new Scanner(file);
      
      
      
      
               File outfile=new File("MyReceipt.txt");
               FileOutputStream fos;
                  fos = new FileOutputStream(outfile);
      
               PrintWriter output=new PrintWriter(fos);
      
               while(read.hasNext()){
                  String item=read.next();
                  double price=read.nextDouble();
                  String status=read.next();
      
                  output.println("My Receipt: ");
                  output.println("--------------------");
      
                  if(status.equals("Done")==true)
                     output.println(item+"  "+price);
      
      
                  double total=0;
      
      
                  total+=price;
                  output.println("--------------------");
                  output.println("total= "+total);
               }
               read.close();
               output.close();
              } catch (FileNotFoundException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
      

      【讨论】:

      • 很抱歉,但这不是处理FileNotFoundException 问题的解决方案,但不是真正的问题(在问题标题中说明):并且文件确实存在。请参阅sgp15's answer 以查看可能的解决方案。
      • 据我了解,他试图保护代码免受 FileNotFoundException 的影响,但失败了。
      • 我从问题中了解到,OP 需要读取文件但正在获取FileNotFoundException,因此作为替代方案,他/她试图处理异常,但这不是解决方案。再次阅读标题:即使我处理了异常,我仍然收到“FileNotFoundException” [这确实是代码设计问题] ,并且文件确实存在 [这是真正的问题]
      • 是的,我一直在阅读并没有插入。顺便说一句,我们正在测试文件 MyList.txt 并尝试读取文件 MyReceipt.txt。这可能是问题吗?
      • 更新页面以刷新问题标题。是的,这也可能是个问题。
      【解决方案4】:
      try {
      
                     if(file.exists()==false)
                      throw new FileNotFoundException("the file input doesn't exist");
                }
                catch(FileNotFoundException e){System.out.print(e.getMessage());}
      
                //I tried handling the exception but it didn't work
      
                Scanner read=new Scanner(file);
      

      这里的最后一行显示,在您的 try-catch 之外是您创建 Scanner read=new Scanner(file); 的地方,问题是,如果该文件不存在,则没有什么可以阻止扫描器在尝试访问此文件时崩溃文件。

      您应该将Scanner read=new Scanner(file); 行放在您的try-catch 中,如下所示:

                  try {
                  if(file.exists()){
                      Scanner read=new Scanner(file);
                  }
                  else if(file.exists()==false)
                      throw new FileNotFoundException("the file input doesn't exist");
                  }
              }
      

      如果文件不存在,这应该可以防止您的程序崩溃。

      【讨论】:

        【解决方案5】:

        尝试使用文件类的 getAbsolutePath() 来查看您在哪个目录中工作,以及您的文件是否存在于同一目录中。否则考虑给出完整路径。

        【讨论】:

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