Java - Temporary File Create by File Object

Introduction

You can create a temporary file in the default temporary file directory or a directory of your choice.

To create a temporary file in the default temporary directory, use the createTempFile() static method from File class.

It accepts a prefix which is at least three characters in length and a suffix to generate the temporary file name.

Demo

import java.io.File;
import java.io.IOException;

public class Main {
  public static void main(String[] args) {
    try {/*w  w  w .j a va 2 s  .  com*/
      File tempFile = File.createTempFile("aaa", ".txt");
      boolean fileCreated = tempFile.exists();
      System.out.println(fileCreated);
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}

Result

Related Topic