[Java] Create a temporary file

Purpose

--I want to create a file in any directory. ――I want to make more than one, so I don't want the file name to be covered. --I want to use the created file temporarily and discard it.

Subtle way

An example of using the current milliseconds to make the file name unique. Of course, it cannot be said that it will not be worn. .. ..

python


//Get the current millisecond
long millTime = System.currentTimeMillis();
String fileName = "tmp_" + millTime + ".txt";
File file = new File(fileName);

Nice way

Use the createTempFile method of the Files class. (Java 7 or later)

python


Files.createTempFile(Paths.get("Arbitrary directory"), "prefix", "suffix");

The following is an example of creating a temporary file, writing it, and then deleting it.

python


File file = null;
try {
  Path tmpPath = Files.createTempFile(Paths.get("/tmp"), "prefix", ".suffix");
  file = tmpPath.toFile();
  
  //Write to file
  FileWriter fw = new FileWriter(file, true);
  BufferedWriter bw = new BufferedWriter(fw);
  PrintWriter pw = new PrintWriter(bw);
  pw.write("Konjac");
  pw.flush();
  pw.close();
} catch (IOException e) {
  e.printStackTrace();
} finally {
 //Delete file
  if (file != null && file.exists()) {
    file.delete();
  }
}

Up to Java6, you can use the createTempFile method of the File class. reference https://docs.oracle.com/javase/jp/8/docs/api/java/io/File.html#createTempFile-java.lang.String-java.lang.String-java.io.File-

Recommended Posts

[Java] Create a temporary file
[Java] Create a filter
Create a java method [Memo] [java11]
[Java] Let's create a mod for Minecraft 1.14.4 [0. Basic file]
[Java] Let's create a mod for Minecraft 1.16.1 [Basic file]
Upload a file using Java HttpURLConnection
Create a Java project using Eclipse
[Java] How to create a folder
[Java] Create and apply a slide master
Create a jar file with the command
Let's create a Java development environment (updating)
Why does Java call a file a class?
Create a TODO app in Java 7 Create Header
Create a temporary class with new Object () {}
java file creation
To create a Zip file while grouping database search results in Java
[Java] Create a jar file with both compressed and uncompressed with the jar command
[Java 7 or later] Prevent temporary file deletion omissions
Read a string in a PDF file with Java
Create a CSR with extended information in Java
Create a simple bulletin board with Java + MySQL
[Windows] [IntelliJ] [Java] [Tomcat] Create a Tomcat9 environment with IntelliJ
Let's create a timed process with Java Timer! !!
Create a Lambda Container Image based on Java 15
[Java] Create something like a product search API
Try to create a bulletin board in Java
[Java] Create a collection with only one element
Let's create a super-simple web framework in Java
[Java] Let's create a mod for Minecraft 1.14.4 [Introduction]
A story about trying to operate JAVA File
Create Scala Seq from Java, make Scala Seq a Java List
A bat file that uses Java in windows
[Java] Let's create a mod for Minecraft 1.16.1 [Introduction]
[Java] Let's create a mod for Minecraft 1.14.4 [99. Mod output]
[Java] File system operation
java (split source file)
java core: chopped core file
java build a triangle
[Note] Java: Create a simple project while learning how the configuration file works.
Let's create a versatile file storage (?) Operation library by abstracting file storage / acquisition in Java
Create JSON in Java
Create a portfolio app using Java and Spring Boot
Create a Java development environment using jenv on Mac
[Java] Let's create a mod for Minecraft 1.14.4 [4. Add tools]
Create a docker image that runs a simple Java app
How to create a Java environment in just 3 seconds
Studying java8 (such as reading a file using Stream)
How to jump from Eclipse Java to a SQL file
[Java] Let's create a mod for Minecraft 1.14.4 [5. Add armor]
[Java] Let's create a mod for Minecraft 1.14.4 [Extra edition]
[Java] Let's create a mod for Minecraft 1.14.4 [7. Add progress]
[Java] Let's create a mod for Minecraft 1.14.4 [6. Add recipe]
[Java] Let's create a mod for Minecraft 1.16.1 [Add item]
Java / Twitter clone / task management system (1) Create a database
[Java] Let's create a mod for Minecraft 1.14.4 [1. Add items]
How to create a data URI (base64) in Java
I tried OCR processing a PDF file with Java
[Note] Create a java environment from scratch with docker
How to convert a file to a byte array in Java
Create a Java, JavaScript team development environment (problem raising)
[Java] Let's create a mod for Minecraft 1.14.4 [2. Add block]