Java IO & NIO - Files.createTempDirectory() 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 createTempDirectory(Path dir,
                                       String prefix,
                                       FileAttribute<?>... attrs)
                                throws IOException

Creates a new directory in the specified directory, using the given prefix to generate its name. The name of the directory generated is implementation dependent



public static Path createTempDirectory(String prefix,
                                       FileAttribute<?>... attrs)
                                throws IOException

Creates a new directory in the default temporary-file directory (system dependent), using the given prefix to generate its name.

This method works in exactly the manner specified by the above method; Files#createTempDirectory(Path,String,FileAttribute[]) method for the case that the dir parameter is the temporary-file directory.


Examples


package com.logicbig.example.files;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class CreateTempDirectoryExample {

public static void main(String... args) throws IOException {
Path path = Files.createTempDirectory("myFile");
System.out.println(path);
boolean b = Files.isDirectory(path);
System.out.println(b);
}
}

Output

C:\Users\Joe\AppData\Local\Temp\myFile5747492692079110850
true




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 CreateTempDirectoryExample2 {

public static void main(String... args) throws IOException {
Path basePath = Paths.get("c:\\myDir");
System.out.printf("base dir: %s, exists: %s%n",
basePath, Files.exists(basePath));

Path path = Files.createTempDirectory(basePath, "test");
System.out.printf("Temp directory created: %s, exists: %s%n",
path, Files.exists(path));
}
}

Output

base dir: c:\myDir, exists: true
Temp directory created: c:\myDir\test4294789233886000390, exists: true




See Also