Try with resources with multiple resources

61 Views Asked by At

I am trying to use try-with-resources with the code below

try {
    FileOutputStream fileIn = new FileOutputStream("veri.obj");
    ObjectOutputStream objectIn = new ObjectOutputStream(fileIn);

    objectIn.writeObject("123"); 
    fileIn.close();
} catch (IOException | ClassNotFoundException e) {
    e.printStackTrace();
}

Like this,

try(FileOutputStream fileIn = new FileOutputStream("veri.obj");
    ObjectOutputStream objectIn = new ObjectOutputStream(fileIn))
{

    objectIn.writeObject("123"); 
} catch (IOException | ClassNotFoundException e) {
    e.printStackTrace();
}

But I’m taking an IOException during writeObject(); The file is exist and there is no problem with r/w permission. The first code is running but second one is not. What am I missing?

I tried to check file existence and permission with methods of File class

0

There are 0 best solutions below