0

I have an array named theDirectory, which holds many DirectoryEntrys, each consisting of a name and telno. I now need to print each DirectoryEntry inside theDirectory into a text file. This is the method I have tried, however i am getting the error: unreported exception IOException; must be caught or declared to be thrown.

My code:

public void save() {
    PrintWriter pw = new PrintWriter(new FileWriter("directory.txt", true)); 

    for (DirectoryEntry x : theDirectory) {
        pw.write(x.getName());
        pw.write(x.getNumber());
        pw.close();
    }
}

any help on this matter would be much appreciated!

Joe Perkins
  • 261
  • 3
  • 9
  • 17
  • 3
    Do you understand about checked exceptions? Read http://docs.oracle.com/javase/tutorial/essential/exceptions/ Also note that you should use a try-with-resources statement or a try/finally statement to close your writer at the end - and *not* inside the loop. – Jon Skeet Apr 10 '14 at 19:23
  • 1
    @JonSkeet I will be reading up on these now thankyou. – Joe Perkins Apr 10 '14 at 19:32

2 Answers2

0

your modified code should look like this :

  public void save() {

PrintWriter pw=null;
    try{
        pw = new PrintWriter(new FileWriter("directory.txt", true)); 

        for (DirectoryEntry x : theDirectory) {
            pw.write(x.getName());
            pw.write(x.getNumber());

        }

    }
    catch(IOException e)
    {
     e.printStackTrace();
    }

finally
{
   pw.close();
}

    }

as mentioned by Jon Skeet

Raju Sharma
  • 2,496
  • 3
  • 23
  • 41
  • yes , it will also run fine, but good is what you told. Thanks, i have modified my answers. – Raju Sharma Apr 10 '14 at 19:34
  • 1
    I would never suggest using `catch (Exception)` like that... we don't know whether the OP should actually declare that their method can throw IOException... and *at least* it should just catch IOException. (I also wouldn't use FileWriter or PrintWriter, but...) – Jon Skeet Apr 10 '14 at 19:35
  • @Jon Skeet, it is generally i used to handle most of the Exception , but really i forgot to mentioned Exception to IOException for PrintWriter class. – Raju Sharma Apr 10 '14 at 19:45
  • 1
    Do you mean you *often* catch `Exception`? Ick! – Jon Skeet Apr 10 '14 at 19:47
  • @Jon Skeet, yes it is, i dont remember as most of the exception throws by statements, so gets help from IDE(Eclipse) or i use Exception generally. – Raju Sharma Apr 10 '14 at 19:52
  • That's a *really* bad idea. That means there are all kinds of exceptions you shouldn't be catching (because they indicate bugs). I rarely *catch* exceptions anyway, preferring to let them propagate up... – Jon Skeet Apr 10 '14 at 19:54
  • Thanks , i will also refer the doc mentioned by you for checked exceptions. – Raju Sharma Apr 10 '14 at 19:57
0

The other answers here have some flaws, this is probably what you want if you're using Java 7 or later:

public void save() {
    try (PrintWriter pw = new PrintWriter(new FileWriter("directory.txt", true))) {         
        for (DirectoryEntry x : theDirectory) {
            pw.write(x.getName());
            pw.write(x.getNumber());                
        }
    }
    catch (IOException ex)
    {
        // handle the exception
    }   
}
Mike B
  • 5,390
  • 2
  • 23
  • 45