25

I'm creating a temporary file in Java but I'm unable to delete it. This is the code I have written:

temp = File.createTempFile("temp", ".txt");
temp.deleteOnExit();
fileoutput = new FileWriter(temp);
buffout = new BufferedWriter(fileoutput);
jps
  • 20,041
  • 15
  • 75
  • 79

4 Answers4

27

Add the following code (after you have done your operations with the file):

buffout.close();
fileoutput.close();
temp.delete();

As long as some stream on the file is open, it is locked (at least on the windows-implementation of the JVM). So it cannot be deleted.

It is good practice always to check if all opened streams get closed again after usage, because this is a bad memory-leak-situation. Your application can even eat up all available file-handles, that can lead to an unusable system.

Mnementh
  • 50,487
  • 48
  • 148
  • 202
  • Was about to write the same thing. As a rule, deleteOnExit() is rather unreliable. I would do the above, but also check the return value from temp.delete() to make sure that all locks are removed. – mikek Jun 22 '09 at 10:43
  • 1
    As Mike said, I would not rely on deleteOnExit(). Additionally the temporary file is kept until the end of the program-execution, even if no longer needed. On a server-application that can be a long time. So explicitly deleting the file is preferable. – Mnementh Jun 22 '09 at 10:50
  • Can you delete the file from Windows Explorer (assuming you're using Windows)? – oxbow_lakes Jun 22 '09 at 11:29
  • By attempting and failing to explicitly delete the tmp files I created, I identified a resource leak in my code (a PrintStream I didn't close). – ewan.chalmers Sep 01 '15 at 13:28
3

There's a bug saying that if the file is open by filewriter or anything, it won't be deleted. On windows. Check if you close your file writers.

Another workaround would be installing a ShutdownHook which would manually delete the file.

Yoni Roit
  • 28,280
  • 7
  • 36
  • 32
0

You have to shut down a VM cleanly in order for the deleteOnExit to work properly (I suspect). On UNIX a kill would be a clean shutdown (i.e. the ShutdownHooks would be processed) whereas a kill -9 would be more like a force quit.

deleteOnExit definitely works for me!

oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449
  • 3
    deleteOnExit keeps the file until the program ends. If the program only executes one action and stops afterwards that may be OK, but on a server-application the tempfiles are kept this way for a long time, accumulate and possibly eating up your inodes and your disk-space. – Mnementh Jun 22 '09 at 10:52
  • In the question, the use of deleteOnExit is not inappropriate - we've no idea what the specific usage pattern is. I would say that deleteOnExit is generally undesirable for a number of reasons (one being that it isn't reversible) – oxbow_lakes Jun 22 '09 at 11:28
0

Code to close the inpustream and outputstream:

    FileInputStream in = new FileInputStream();

     ArrayList list_in = new ArrayList<FileInputStream>();

     list_in.add(in);

     FileOutputStream out = new FileOutputStream();

     ArrayList list_out = new ArrayList<OutputputStream>();

     list_in.add(out);

     public do_before_exit()
     {

      for(int i=0;i<list_in.size();i++)
      {
      FileInputStream in=(FileInputStream)list_in.get(i)
       FileInputStream out=(FileInputStream)list_out.get(i)

      in.close()
    out.close();
   }

}
Eric Aya
  • 69,473
  • 35
  • 181
  • 253