4

The standard way of handling file reading and writing in java goes something like this:

try
{
    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("file.dat"));
    oos.writeObject(h);
    oos.close();
}
catch (FileNotFoundException ex)
{
}
catch (IOException ex)
{
}

But I'm bothered by that code, because it could be possible here that the file is never closed if an exception is thrown. Sure we could add a finally clause and initialise the ObjectOutputStream outside the try block. However, when you do that you need to add another try/catch block INSIDE the finally block again...that's just ugly. Is there a better way of handling this problem?

3
  • Adding another try/catch block helps you isolate the problem when an exception occurs Commented Feb 25, 2011 at 21:32
  • 1
    @Alpine it doesn't help you any more than the information provided by the stack trace *shrug* Commented Feb 25, 2011 at 21:35
  • @Nathan Hughes: Bad formulation of my part, am aware of the dangers, but thanks for pointing that out. Commented Feb 25, 2011 at 21:53

5 Answers 5

8

use apache commons io

http://commons.apache.org/proper/commons-io/

look at their FileUtils class. Full of gold. Gold I say....

Sign up to request clarification or add additional context in comments.

2 Comments

Apache commons is the best, no need to write nested try catch
That link has expired!
6

This is not the standard way at all. This is the bad way.

The way I use most of the time is this one :

ObjectOutputStream out = null;
try {
    out = new ObjectOutputStream(new FileOutputStream("file.dat"));
    // use out
}
finally {
    if (out != null) {
        try {
            out.close();
        }
        catch (IOException e) {
            // nothing to do here except log the exception
        }
    }
}

The code in the finally block can be put in a helper method, or you can use commons IO to close the stream quietly, as noted in other answers.

A stream must always be closed in a finally block.

Note that JDK7 will make it much easier with the new syntax, which will automatically close the stream at the end of the try block :

try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("file.dat"))) {
    // use out
}

2 Comments

Am aware that not closing it in the finally block is risky, hence my question. But naming it the standard way was indeed a bad formulation.However the JDK7 info is new to me, and looks interesting. Is this comparable to the using{} syntax that C# uses in case you or someone know both languages?
I don't know about C#, but it's like Python's with syntax. python.org/dev/peps/pep-0343
3

This why I use commons-io's IOUtils.closeQuitely(...)

try
{
...
}
finally 
{
   IOUtils.closeQuietly(costream);
}

Comments

0
try
{
    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("file.dat"));
    oos.writeObject(h);
    //oos.close(); // glow coder removed
}
catch (FileNotFoundException ex)
{
}
catch (IOException ex)
{
}
// glowcoder adds:
finally {
    try {
        oos.close();
    }
    catch(IOException ex) {
        // dammit we tried!
    }
}

3 Comments

Works, but this is just what I wanted to avoid doing.
@Rene what exactly are you trying to avoid, and why are you trying to avoid it?
As I wrote in my question, I just think this way is ugly, creating a huge block of code, for handling exceptions on just 3 statements. So my question was purely about coding style, maybe I'm too fussy about it ;) Thanks for your response anyway :)
0

add this finally:

finally{
   if(oos != null)
      oos.close();
}

1 Comment

you'll need to add another try...catch here and that's just what i wanted to avoid

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.