Answer by DigitShifter for How to create a temporary directory/folder in Java?
Try this small example:Code:try { Path tmpDir = Files.createTempDirectory("tmpDir"); System.out.println(tmpDir.toString()); Files.delete(tmpDir);} catch (IOException e) {...
View ArticleAnswer by geri for How to create a temporary directory/folder in Java?
Before Java 7 you could also:File folder = File.createTempFile("testFileUtils", ""); // no suffixfolder.delete();folder.mkdirs();folder.deleteOnExit();
View ArticleAnswer by Arne for How to create a temporary directory/folder in Java?
Just for completion, this is the code from google guava library. It is not my code, but I think it is valueable to show it here in this thread. /** Maximum loop count when creating temp directories. */...
View ArticleAnswer by Carsten Engelke for How to create a temporary directory/folder in...
As you can see in the other answers, no standard approach has arisen.Hence you already mentioned Apache Commons, I propose the following approach using FileUtils from Apache Commons IO:/** * Creates a...
View ArticleAnswer by immoteous for How to create a temporary directory/folder in Java?
I got the same problem so this is just another answer for those who are interested, and it's similar to one of the above:public static final String tempDir =...
View ArticleAnswer by Andres Kievsky for How to create a temporary directory/folder in Java?
This is the source code to the Guava library's Files.createTempDir(). It's nowhere as complex as you might think:public static File createTempDir() { File baseDir = new...
View ArticleAnswer by seeker for How to create a temporary directory/folder in Java?
As of Java 1.7 createTempDirectory(prefix, attrs) and createTempDirectory(dir, prefix, attrs) are included in java.nio.file.FilesExample:File tempDir = Files.createTempDirectory("foobar").toFile();
View ArticleAnswer by Greg Price for How to create a temporary directory/folder in Java?
Naively written code to solve this problem suffers from race conditions, including several of the answers here. Historically you could think carefully about race conditions and write it yourself, or...
View ArticleAnswer by Spina for How to create a temporary directory/folder in Java?
The Google Guava library has a ton of helpful utilities. One of note here is the Files class. It has a bunch of useful methods including:File myTempDir = Files.createTempDir();This does exactly what...
View ArticleAnswer by matsev for How to create a temporary directory/folder in Java?
If you need a temporary directory for testing and you are using jUnit, @Rule together with TemporaryFolder solves your problem:@Rulepublic TemporaryFolder folder = new TemporaryFolder();From the...
View ArticleAnswer by Chris Lott for How to create a temporary directory/folder in Java?
I like the multiple attempts at creating a unique name but even this solution does not rule out a race condition. Another process can slip in after the test for exists() and the if(newTempDir.mkdirs())...
View ArticleAnswer by Developer Dude for How to create a temporary directory/folder in Java?
Do not use deleteOnExit() even if you explicitly delete it later.Google 'deleteonexit is evil' for more info, but the gist of the problem is:deleteOnExit() only deletes for normal JVM shutdowns, not...
View ArticleAnswer by Keith for How to create a temporary directory/folder in Java?
This is what I decided to do for my own code:/** * Create a new temporary directory. Use something like * {@link #recursiveDelete(File)} to clean this directory up since it isn't * deleted...
View ArticleAnswer by ordnungswidrig for How to create a temporary directory/folder in Java?
Using File#createTempFile and delete to create a unique name for the directory seems ok. You should add a ShutdownHook to delete the directory (recursively) on JVM shutdown.
View ArticleAnswer by TofuBeer for How to create a temporary directory/folder in Java?
If you are using JDK 7 use the new Files.createTempDirectory class to create the temporary directory.Path tempDirWithPrefix = Files.createTempDirectory(prefix);Before JDK 7 this should do it:public...
View ArticleAnswer by matt b for How to create a temporary directory/folder in Java?
This code should work reasonably well:public static File createTempDir() { final String baseTempPath = System.getProperty("java.io.tmpdir"); Random rand = new Random(); int randomInt = 1 +...
View ArticleAnswer by Michael Myers for How to create a temporary directory/folder in Java?
As discussed in this RFE and its comments, you could call tempDir.delete() first. Or you could use System.getProperty("java.io.tmpdir") and create a directory there. Either way, you should remember to...
View ArticleAnswer by Paul Tomblin for How to create a temporary directory/folder in Java?
Well, "createTempFile" actually creates the file. So why not just delete it first, and then do the mkdir on it?
View ArticleHow to create a temporary directory/folder in Java?
Is there a standard and reliable way of creating a temporary directory inside a Java application? There's an entry in Java's issue database, which has a bit of code in the comments, but I wonder if...
View Article
More Pages to Explore .....