Java Tutorial/File/Temporary File

Материал из Java эксперт
Перейти к: навигация, поиск

Create a temporary file

import java.io.File;
public class Main{
  public static void main(String[] args) throws Exception{
    File f = File.createTempFile("temp_", null);
    
    System.out.println(f.getAbsolutePath());
    
    f.deleteOnExit();
  }
}





Create temporary file in specified directory

import java.io.File;
public class Main {
  public static void main(String[] args) throws Exception {
    File file = null;
    File dir = new File("C:/");
    file = File.createTempFile("JavaTemp", ".javatemp", dir);
    System.out.println(file.getPath());
  }
}
//C:\JavaTemp5893283162648915021.javatemp





Create temporary file with specified extension suffix

import java.io.File;
public class Main {
  public static void main(String[] args) throws Exception {
    File file1 = null;
    File file2 = null;
    file1 = File.createTempFile("JavaTemp", null);
    file2 = File.createTempFile("JavaTemp", ".java");
    System.out.println(file1.getPath());
    System.out.println(file2.getPath());
  }
}