Java IO & NIO Java Java API
java.nio.file.Files
public static Path createTempFile(Path dir, String prefix, String suffix, FileAttribute<?>... attrs) throws IOException
Creates a new empty file in the specified directory, using the given prefix and suffix strings to generate its name.
dir
prefix
null
suffix
.tmp
attrs
public static Path createTempFile(String prefix, String suffix, FileAttribute<?>... attrs) throws IOException
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)); }}
temp file created: D:\my-file8701937624504860042.txttemp file exits: truetemp 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)); }}
temp file created: C:\Users\Joe\AppData\Local\Temp\my-file10138973897240565218.txttemp file exits: truetemp file size: 0