views:

2028

answers:

4

Duplicate: stackoverflow.com/questions/375910

Is there a way of creating a temporary folder in java ? I know of File's static method createTempFile, but this will only give me a temporary file.

+4  A: 

I've never seen a good solution for this, but this is how I've done it.

File temp = File.createTempFile(...);
temp.delete();
temp.mkdir();
John Meagher
This is an interesting approach. I didn't think about it this way.
Geo
+3  A: 

I write my own utility classes for creating temporary directories and for disposing them when they are not anymore needed. For example like this.

Esko Luontola
+4  A: 

Any reason you can't use the directory defined by the java.io.tmpdir property?

ie

String dirName = System.getProperty("java.io.tmpdir");
cagcowboy
'Temporary file' from createTempFile is automatically deleted when JVM exits. I think OP is asking for this kind of directory, so using existing tmpdir directory won't make it. (I needed something similar for writing unit tests, and used createTempFile+delete+mkdir and created only 'temporary' files within this directory -- JVM can then do the cleanup, if I remember correctly)
Peter Štibraný
Ok, it's not deleted automatically .. you need to ask JVM first to do so (by deleteOnExit)
Peter Štibraný
Just as a side note: you could easily add 'destruction on JVM exit' yourself by registering a shutdown hook.
disown
+6  A: 

I would check out this past question in SO for a solution. Or this one!

Brian Agnew