Deleting a Temporary File with the deleteOnExit() Method - Java File Path IO

Java examples for File Path IO:File Permission

Description

Deleting a Temporary File with the deleteOnExit() Method

Demo Code

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

public class Main {
  public static void main(String[] args) throws Exception {
    Path basedir = FileSystems.getDefault().getPath("C:/folder1/tmp");
    String tmp_file_prefix = "test_";
    String tmp_file_sufix = ".txt";

    final Path tmp_file = Files.createTempFile(basedir, tmp_file_prefix,
        tmp_file_sufix);// w  w w . j  ava  2 s  .  c  o m

    File asFile = tmp_file.toFile();
    asFile.deleteOnExit();

    Thread.sleep(10000);

  }
}

Related Tutorials