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
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class GFG {
public static void main(String[] args)
{
String prefix = "TempDirectory" ;
Path dir = (Path)Paths.get( "/usr" , "local" , "bin" ,
"directory" );
try {
Path tempDir
= Files.createTempDirectory(dir, prefix);
System.out.println(
"Temporary Directory created at:\n"
+ tempDir.toString());
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
import java.nio.file.Files;
import java.nio.file.Path;
public class GFG {
public static void main(String[] args)
{
String prefix = "TempDirectory" ;
try {
Path tempDir
= Files.createTempDirectory(prefix);
System.out.println(
"Temporary Directory created at:\n"
+ tempDir.toString());
tempDir.toFile().deleteOnExit();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
|
Output:
Temporary Directory created at:
/var/folders/13/jsq29lh11bn7kgwnzpggqytr0000gn/T/TempDirectory4403940384431706243
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
23 Jun, 2021
Like Article
Save Article