how can I create a temporary folder in java 6? [duplicate]
Asked Answered
C

4

9

Possible Duplicate:
Create a temporary directory in Java

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.

Conservatism answered 3/5, 2009 at 16:19 Comment(1)
Why isn't this question flagged as duplicate? Brian Agnew has posted the relevant links.Bickford
R
30

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

File temp = File.createTempFile("folder-name","");
temp.delete();
temp.mkdir();
Rexford answered 3/5, 2009 at 16:22 Comment(1)
This is an interesting approach. I didn't think about it this way.Conservatism
R
8

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

ie

String dirName = System.getProperty("java.io.tmpdir");
Reductive answered 3/5, 2009 at 16:32 Comment(3)
'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)Threecornered
Ok, it's not deleted automatically .. you need to ask JVM first to do so (by deleteOnExit)Threecornered
Just as a side note: you could easily add 'destruction on JVM exit' yourself by registering a shutdown hook.Isaiah
P
5

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

Primogeniture answered 3/5, 2009 at 17:5 Comment(0)
L
4

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

Liatris answered 3/5, 2009 at 16:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.