unreported exception java.io.FileNotFoundException

2019-09-04 01:28发布

问题:

We keep running this error. The goal is to read the data from the array into an output file. Thanks for the help!

public static void save(Salesperson[] array)

  {
     PrintStream outfile = null;
     try
     {
        outfile = new PrintStream(new FileOutputStream("data.txt"));
     }
        catch(FileNotFoundException e)
        {
           JOptionPane.showMessageDialog(null,"The file could not be created.");
        }
         System.setOut(new PrintStream(new FileOutputStream("output.txt")));
     for(int k = 0; k < array.length; k++)
     {
        System.out.println(array[k]);
     }

     outfile.close();
     System.out.println("Saved.");

  }

回答1:

You are getting the error because there is a checked exception associated with FileOutputStream which you are not catching/throw-declaring.