Java IO Tutorial - Java Files .createTempDirectory (String prefix, FileAttribute <?> . . . attrs)








Syntax

Files.createTempDirectory(String prefix, FileAttribute <?>... attrs) has the following syntax.

public static Path createTempDirectory(String prefix,    FileAttribute <?>... attrs)    throws IOException

Example

In the following code shows how to use Files.createTempDirectory(String prefix, FileAttribute <?>... attrs) method.

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
//from  www  . j  av  a2 s  . co m
public class Main {

    public static void main(String[] args) {

        Path basedir = FileSystems.getDefault().getPath("C:/tutorial/tmp/");
        String tmp_dir_prefix = "Swing_";

        //get the default temporary folders path
        String default_tmp = System.getProperty("java.io.tmpdir");
        System.out.println(default_tmp);

        try {
            //set a prefix
            Path tmp_2 = Files.createTempDirectory(tmp_dir_prefix);
            System.out.println("TMP: " + tmp_2.toString());

        } catch (IOException e) {
            System.err.println(e);
        }
    }
}

The code above generates the following result.