Java IO & NIO - Files.createTempFile() Examples

Code Snippets Java IO & NIO Java 


Class:

java.nio.file.Files

java.lang.Objectjava.lang.Objectjava.nio.file.Filesjava.nio.file.FilesLogicBig

Methods:

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.

Parameters:
dir - the path to directory in which to create the file
prefix - the prefix string to be used in generating the file's name; may be null
suffix - the suffix string to be used in generating the file's name; may be null , in which case .tmp is used
attrs - an optional list of file attributes to set atomically when creating the file
Returns:
the path to the newly created file that did not exist before this method was invoked



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.

Parameters:
prefix - the prefix string to be used in generating the file's name; may be null
suffix - the suffix string to be used in generating the file's name; may be null , in which case .tmp is used
attrs - an optional list of file attributes to set atomically when creating the file
Returns:
the path to the newly created file that did not exist before this method was invoked


Examples


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




See Also