How to create temporary folders in Java

Asked 1 years ago, Updated 1 years ago, 81 views

Is there a standard or a good way to create a temporary directory within a Java application?

file java file-io directory temporary-directory

2022-09-22 22:26

1 Answers

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.


2022-09-22 22:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.