0

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.");

  }
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • 4
    Okay, so what don't you understand about it? It seems fairly clear to me. How much do you understand about checked exceptions? And do you *really* need to redirect `System.out`? (Why not just write to the file, without the redirection?) – Jon Skeet May 11 '13 at 15:47
  • Read the [Java tutorial about exceptions](http://docs.oracle.com/javase/tutorial/essential/exceptions/), or your Java introductory book. – JB Nizet May 11 '13 at 15:51

1 Answers1

0

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

ValarDohaeris
  • 6,064
  • 5
  • 31
  • 43
  • Specifically which exception? I understand the problem now but I'm not sure which exception it's missing... – user2373182 May 11 '13 at 16:05
  • You are not handling `FileNotFoundException` for your second `new FileOutputStream()`. – ValarDohaeris May 11 '13 at 16:09
  • @user2373182: the exception message says: unreported exception **java.io.FileNotFoundException**; must be caught or declared to be thrown. Read the exception message. – JB Nizet May 11 '13 at 16:47