Creating Temporary Files with the createTempFile() method - Java File Path IO

Java examples for File Path IO:File Permission

Description

Creating Temporary Files with the createTempFile() method

Demo Code

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

public class Main {
  public static void main(String[] args) {
    String tmp_file_prefix = "test_";
    String tmp_file_sufix = ".txt";

    try {//from  ww  w  . java2  s. co  m
      // passing null prefix/suffix
      Path tmp_1 = Files.createTempFile(null, null);
      System.out.println("TMP: " + tmp_1.toString());

      // set a prefix and a suffix
      Path tmp_2 = Files.createTempFile(tmp_file_prefix, tmp_file_sufix);
      System.out.println("TMP: " + tmp_2.toString());

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

  }
}

Related Tutorials