0

I'm using Java in a really big application and some time i have to use temp file. Those file i want to be deleted at the application close, this is a simple snapshot i'm using:

File tempFile = File.createTempFile("sign_", "tmp.pdf");
tempFile.deleteOnExit();

I'm not reporting all the code since is really big and i have many class working each other. I would know which could be the reason that avoid the delete on closure of certain file (some file are deleted other not, but they came always from the same piece of code the one that are not deleted).

Edit: i have already read this example but i think i need some "theoric" motivation and not code example to find the reason.

Community
  • 1
  • 1
Razlo3p
  • 455
  • 2
  • 7
  • 28
  • 1
    ["Deletion will be attempted only for normal termination of the virtual machine, as defined by the Java Language Specification."](https://docs.oracle.com/javase/7/docs/api/java/io/File.html#deleteOnExit()) Is the JVM exiting abnormally? – Andy Turner Feb 23 '16 at 14:14
  • 3
    If that is your real code wouldn't you need to set tmp.deleteOnExit(); to tempFile.deleteOnExit()? – Alex Feb 23 '16 at 14:15
  • @AlexClem i copied from another piece of code, you are right! I'm editing – Razlo3p Feb 23 '16 at 14:16
  • @AndyTurner i think yes, but how can i be sure? (other file created and delete the same way are regulary deleted) – Razlo3p Feb 23 '16 at 14:16
  • Does it exit with a zero return code? Also, if you are on windows, [this answer](http://stackoverflow.com/questions/16691437/when-are-java-temporary-files-deleted/16694493#16694493) states that "leaving the temp file unclosed is enough to keep it from being deleted". – Andy Turner Feb 23 '16 at 14:19
  • @AndyTurner yes it returns zero. The file are visualized into a pdf viewer (ICE PDF) and i don't know how they are destroyed at close. – Razlo3p Feb 23 '16 at 14:29
  • @fge Since i need a reliable way to create temporary file and manage trougth all the application. As i stated is really big and i found this way to manage temporary file. If you know a better wayt to avoid to do it manually (like reinventing the weel) i'm happy to learn – Razlo3p Feb 23 '16 at 14:36

1 Answers1

2

The method "deleteOnExit()" only works if the VM terminates normally. If the VM crash or forced termination the file might remain undeleted.

I don't know how it is implemented, but you could try to put the tempFile.deleteOnExit() inside the finally.

File tempFile = null; 
try{            
    tempFile = File.createTempFile("sign_", "tmp.pdf");

}catch(IOException e){          
    e.printStackTrace();            
} finally {
    if (tempFile != null) {
        tempFile.deleteOnExit();
        tempFile = null;
        //Added a call to suggest the Garbage Collector
        //To collect the reference and remove
        System.gc(); 
    }
}

Or maybe, close all the references to the file and then call "File.delete()" to delete immediate.

If anyone is working, problably some reference to the file exists. In this way, you can try to force the file to be deleted using the org.apache.commons.io.FileUtils.

Example org.apache.commons.io.FileUtils:

File tempFile = null; 
try{            
    tempFile = File.createTempFile("sign_", "tmp.pdf");

}catch(IOException e){          
    e.printStackTrace();            
} finally {
    if (tempFile != null) {
        FileUtils.forceDelete(tempFile);
        System.out.println("File deleted");
    }
}

Example org.apache.commons.io.FileDeleteStrategy:

File tempFile = null; 
try{            
    tempFile = File.createTempFile("sign_", "tmp.pdf");

}catch(IOException e){          
    e.printStackTrace();            
} finally {
    if (tempFile != null) {
        FileDeleteStrategy.FORCE.delete(tempFile);
        System.out.println("File deleted");
    }
}
Brother
  • 2,150
  • 1
  • 20
  • 24
  • I updated the answer including a call to Garbage Collector after setting tempFile = null to try collect the reference. And also, included 2 diferent ways to FORCE the delete – Brother Feb 23 '16 at 16:15
  • with this approach i have still an exception: Unable to delete file: C:\Users\USERNAME\AppData\Local\Temp\tmp.pdf – Razlo3p Feb 26 '16 at 13:32