Wednesday, February 6, 2013

Creating a temporary file (to be deleted after immediate use) {good coding practices}

Creating a temporary file (to be deleted after immediate use) {good coding practices}

File f = null;
try {
    f = createNewFile();
    // do your stuff here.
}
catch (Exception e) {

}
finally {
     if( f != null ) {
         f.delete();
     }
}

This way, we always ensure that file is deleted before use. and any return statements within try{} block will still doesn't prevent file from deleting.

No comments:

Post a Comment