Java IO Tutorial - Java Files.createTempFile(Path dir, String prefix, String suffix, FileAttribute <?>... attrs)








Syntax

Files.createTempFile(Path dir, String prefix, String suffix, FileAttribute <?>... attrs) has the following syntax.

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

Example

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

//from   ww w. jav  a  2  s  .  co  m

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;


public class Main {

    public static void main(String[] args) {

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

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

        try {
            //create a tmp file in a the base dir
            Path tmp_3 = Files.createTempFile(basedir, tmp_file_prefix, tmp_file_sufix);
            System.out.println("TMP: " + tmp_3.toString());

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

The code above generates the following result.