The createTempDirectory() method of java.nio.file.Files Class is used to create a new directory using the given prefix and attributes. The given prefix acts as the name of the formed directory, may be null. The directory is set with given attributes.
Based on the type of arguments passed, the Files class provides 2 types of createTempDirectory() method.
Methods:
1. public static Path createTempDirectory(Path dir, String prefix, FileAttribute<?>… attrs): This method is used to create a new directory in an already existing directory, using the given prefix to generate its name. The path of the newly formed directory is associated with the default FileSystem. Which is the same as the path of the specified directory. The candidate names are generated using the prefix. The File.deleteOnExit() method is used to delete the directory automatically. The attrs parameter is used to set attributes of the directory. Each attribute is identified by its name. If there is more than one attributes is specified with the same name then all the occurrences except the last one are ignored.
Parameters:
- dir – the path to the already existing directory in which the new directory has to be created
- prefix – the name of the directory, may be null
- attrs – set attributes of the directory(optional)
Returns: path of the newly formed directory
Exception:
- IllegalArgumentException – if the prefix is not an appropriate candidate directory name
- UnsupportedOperationException – if any of the specified attributes can not be set to the directory
- IOException – if an I/O error occurs
- SecurityException – if checkWrite method is invoked by the security manager or write permission is denied while creating a directory.
Java
// Java Program to show the usage of // public static Path createTempDirectory import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; // Driver class public class GFG { // main method public static void main(String[] args) { // prefix used to create candidate names String prefix = "TempDirectory" ; // path of the directory in which temporary // directory has to created Path dir = (Path)Paths.get( "/usr" , "local" , "bin" , "directory" ); try { // creating temporary directory ans storing its // path in tempDir variable Path tempDir = Files.createTempDirectory(dir, prefix); // printing the path of the created temporary // directory System.out.println( "Temporary Directory created at:\n" + tempDir.toString()); // deleting the temporary directory after the // virtual machine terminates tempDir.toFile().deleteOnExit(); } catch (Exception e) { e.printStackTrace(); } } } |
Output:
Temporary Directory created at: /usr/local/bin/directory/TempDirectory1472243991754401317
2. public static Path createTempDirectory(String prefix, FileAttribute<?>… attrs): This method is used to create a new directory using the given prefix and attributes. The given prefix acts as the name of the formed directory, may be null. The candidate names are generated using the prefix. The directory is set with given attributes. The path of the newly formed directory is associated with the default FileSystem. The attrs parameter is used to set attributes of the directory. Each attribute is identified by its name. If there is more than one attributes is specified with the same name then all the occurrence except the last one is ignored.
Parameters:
- prefix – the name of the directory, may be null
- attrs – set attributes of the directory(optional)
Returns: path of the newly formed directory
Exception:
- IllegalArgumentException – if the prefix is not an appropriate candidate directory name
- UnsupportedOperationException – if any of the specified attributes can not be set to the directory
- IOException – if an I/O error occurs
- SecurityException – if checkWrite method is invoked by the security manager or write permission is denied while creating a directory
Java
// Java Program to show the usage of // public static Path createTempDirectory( // String prefix, FileAttribute<?>... attrs) // Method import java.nio.file.Files; import java.nio.file.Path; // Driver class public class GFG { // main method public static void main(String[] args) { // prefix used to create candidate names String prefix = "TempDirectory" ; try { // creating temporary directory and storing its // path in tempDir variable Path tempDir = Files.createTempDirectory(prefix); // printing the path of the created temporary // directory System.out.println( "Temporary Directory created at:\n" + tempDir.toString()); // deleting the temporary directory after the // virtual machine terminates tempDir.toFile().deleteOnExit(); } catch (Exception e) { e.printStackTrace(); } } } |
Output:
Temporary Directory created at: /var/folders/13/jsq29lh11bn7kgwnzpggqytr0000gn/T/TempDirectory4403940384431706243