Creates a new empty file in the specified directory, using the given prefix and suffix strings to generate its name.
Creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its
name.
package com.logicbig.example.files;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class CreateTempFileExample {
public static void main(String... args) throws IOException {
Path tempFile = Files.createTempFile(
Paths.get("D:\\"), "my-file", ".txt");
System.out.println("temp file created: " + tempFile);
boolean exists = Files.exists(tempFile);
System.out.println("temp file exits: " + exists);
System.out.println("temp file size: "+ Files.size(tempFile));
}
}
Output
temp file created: D:\my-file8701937624504860042.txt
temp file exits: true
temp file size: 0
package com.logicbig.example.files;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class CreateTempFileExample2 {
public static void main(String... args) throws IOException {
Path tempFile = Files.createTempFile("my-file", ".txt");
System.out.println("temp file created: " + tempFile);
boolean exists = Files.exists(tempFile);
System.out.println("temp file exits: " + exists);
System.out.println("temp file size: " + Files.size(tempFile));
}
}
Output
temp file created: C:\Users\Joe\AppData\Local\Temp\my-file10138973897240565218.txt
temp file exits: true
temp file size: 0