Why might a crash happen?
finally{
try{
bfw.close(); <== exception occured here
pw.close(); <== this is not execute
}catch(Exception e){
e.printStackTrace();
}
}
See your finally block,
What if an exception occurs in bfw.close();?
`pw.close();` will never execute. And this leads to resource leak
How can I ensure to close FileStream?
- , - try catch.
, , , Apache commons IO package
:
try{
........
} finally {
IOUtils.closeQuietly(bfw);
IOUtils.closeQuietly(pw);
}
, java 7