Is there a standard or a good way to create a temporary directory within a Java application?
file java file-io directory temporary-directory
In JDK7, you can create a temporary directory with the File.createTempDirectory class.
And before JDK7 came out,
public static File createTempDirectory()
throws IOException
{
final File temp;
temp = File.createTempFile("temp", Long.toString(System.nanoTime()));
if(!(temp.delete()))
{
throw new IOException("Could not delete temp file: " + temp.getAbsolutePath());
}
if(!(temp.mkdir()))
{
throw new IOException("Could not create temp directory: " + temp.getAbsolutePath());
}
return (temp);
}
I did it like this.
© 2025 OneMinuteCode. All rights reserved.