java继续尝试,直到没有更多的filenotfoundexception

时间:2016-12-09 20:56:46

标签: java input ioexception

我正在尝试编写能够读取输入文件并创建输出文件的代码。但是当我尝试添加一个尝试直到输入正确的输入文件名时,我遇到了问题。它显示在try ....中没有正确的filenotfound异常。

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

          //prompt for the input file name
          Scanner in = new Scanner(System.in);
          //keep trying until there are no more exceptions
          //boolean done = false;
          String inputfilename = " ";
          while (!done)
          {
             try
             {
               System.out.print("Input file name (from your computer): ");
               inputfilename = in.next();
               done = true;
             }
             catch (FileNotFoundException exception)
             {
               System.out.println("****** ERROR ******\nCannot locate the input file '" + inputfilename + "' on your computer - please try again.");
             }
          }
          //prompt for the output file name
          System.out.print("What would you like to call your output file: ");
          //use outputfilename variable to hold input value;
          String outputfilename = in.next();
          //construct the Scanner and PrintWriter objects for reading and writing
          File inputfile = new File(inputfilename);
          Scanner infile = new Scanner(inputfile);
          PrintWriter out = new PrintWriter(outputfilename);
          //read the input and write the output
          out.println("Here is the class average for mstu4031:\n");
          double totalgrade = 0;
          double number = 0;
          while (infile.hasNextDouble())
          {
             double grade = infile.nextDouble();
             out.println("\n");
             out.printf("%.1f\n",grade);
             number++;
             totalgrade = totalgrade + grade;
          }
          //print numbers and average in output file
          out.println("\n\n");
          out.printf("\nNumber of grades: %.1f",number);
          //calculate average
          double average = totalgrade/number;
          out.println("\n\n");
          out.printf("\nAverage: %.2f",average);

          finally
          {     
          in.close();
          out.close();
          }
}

4 个答案:

答案 0 :(得分:1)

try块中没有可能抛出FileNotFoundException

的方法

尝试在try块中实例化您的扫描仪。如果从stdin读取的文件名不存在,它将抛出预期的FileNotFoundException

String inputfilename = null;
Scanner infile = null;
while (!done)
{
   try
   {
     System.out.print("Input file name (from your computer): ");
     inputfilename = in.next();
     infile = new Scanner(new File(inputfilename));
     done = true;
   }
   catch (FileNotFoundException exception)
   {
     System.out.println("****** ERROR ******\nCannot locate the input file '" + inputfilename + "' on your computer - please try again.");
   }
}

答案 1 :(得分:0)

这里错了。您只接收输入而不检查文件是否确实存在。每个有效的输入都可以让你摆脱循环。

       if(new File(inputfilename).exist()){
           done = true;
       }else{
           System.out.println("****** ERROR ******\nCannot locate the input file '" + inputfilename + "' on your computer - please try again.");
       }

答案 2 :(得分:0)

如果try块中的某些内容可能会引发异常,则只能捕获异常。

但是,您应该测试是否存在File.exists()的文件,而不是捕获异常。

File file;
do {
    System.out.print("Input file name (from your computer): ");
    file = new File(in.next());
} while (!file.exists());

答案 3 :(得分:0)

打开文件可能会抛出异常。这就是为什么你需要将它们放入try块中。你只把读取输入部分放在try-catch块

希望此代码正常运行:

//prompt for the input file name
  Scanner in = new Scanner(System.in);
  //keep trying until there are no more exceptions
  //boolean done = false;
  String inputfilename = " ";
  while (!done)
  {
     try
     {
       System.out.print("Input file name (from your computer): ");
       inputfilename = in.next();
       done = true;
       //prompt for the output file name
       System.out.print("What would you like to call your output file: ");
       //use outputfilename variable to hold input value;
       String outputfilename = in.next();
      //construct the Scanner and PrintWriter objects for reading and writing
      File inputfile = new File(inputfilename);
      Scanner infile = new Scanner(inputfile);
      PrintWriter out = new PrintWriter(outputfilename);
      //read the input and write the output
      out.println("Here is the class average for mstu4031:\n");
      double totalgrade = 0;
     double number = 0;
     while (infile.hasNextDouble())
     {
     double grade = infile.nextDouble();
     out.println("\n");
     out.printf("%.1f\n",grade);
     number++;
     totalgrade = totalgrade + grade;
   }
    //print numbers and average in output file
    out.println("\n\n");
    out.printf("\nNumber of grades: %.1f",number);
    //calculate average
    double average = totalgrade/number;
    out.println("\n\n");
    out.printf("\nAverage: %.2f",average);
     }
     catch (FileNotFoundException exception)
     {
       System.out.println("****** ERROR ******\nCannot locate the input file '" + inputfilename + "' on your computer - please try again.");
     }
  }
  finally
  {     
  in.close();
  out.close();
  }