How can I create an empty folder in Java?
How to create a folder in Java?
Asked Answered
Hmm.. I Googled and this was the first result (; –
Coadjutrix
@CoolBeans The creators of StackOverflow have said they want questions here to be the first hit on Google. Nothing wrong with creating a simple google-able question here if it is clearly stated and original (not a duplicate on StackOverflow). –
Grating
File f = new File("C:\\TEST");
try{
if(f.mkdir()) {
System.out.println("Directory Created");
} else {
System.out.println("Directory is not created");
}
} catch(Exception e){
e.printStackTrace();
}
Just wondering: What case would trigger the else-branch? Wouldn't there be an exception whenever the creation fails? –
Pantheon
Isn't it
mkdir
(all lower case)? @0xA3: mkdir throws SecurityExceptions, but surely that won't happen if C:\TEST doesn't exist? –
Questionless @OxA3 If you don't have right to create the directory, the else branch is executed. –
Pavlov
There's also
mkdirs
which will create parent folders too: docs.oracle.com/javase/7/docs/api/java/io/File.html#mkdirs() –
Hagiarchy Please don't use
catch(Exception e){e.printStackTrace()}
: today.java.net/article/2006/04/04/… –
Allen Call File.mkdir
, like this:
new File(path).mkdir();
With Java 7 and newer you can use the static Files.createDirectory() method of the java.nio.file.Files
class along with Paths.get
.
Files.createDirectory(Paths.get("/path/to/folder"));
The method Files.createDirectories() also creates parent directories if these do not exist.
Files.createDirectories()
will also silently ignore already existing directories. –
Isosceles @Isosceles This is only valid for parent directories. If the main directory (in this case "folder") already exists, it will thrown an exception (see also javadoc). –
Cyanamide
Use mkdir()
:
new File('/path/to/folder').mkdir();
Use the mkdir method on the File class:
https://docs.oracle.com/javase/1.5.0/docs/api/java/io/File.html#mkdir%28%29
Using Java 8:
Files.createDirectories(Paths.get("/path/to/folder"));
Same:
new File("/path/to/folder").mkdirs();
Or
Files.createDirectory(Paths.get("/path/to/folder"));
Same:
new File("/path/to/folder").mkdir();
Better to use mkdirs
as:
new File("dirPath/").mkdirs();
mkdirs
: also create parent directories if these do not exist.
ps: don't forget the ending /
that shows explicitly you want to make a directory.
The following code would be helpful for the creation of single or multiple directories:
import java.io.File;
public class CreateSingleOrMultipleDirectory{
public static void main(String[] args) {
//To create single directory
File file = new File("D:\\Test");
if (!file.exists()) {
if (file.mkdir()) {
System.out.println("Folder/Directory is created successfully");
} else {
System.out.println("Directory/Folder creation failed!!!");
}
}
//To create multiple directories
File files = new File("D:\\Test1\\Test2\\Test3");
if (!files.exists()) {
if (files.mkdirs()) {
System.out.println("Multiple directories are created successfully");
} else {
System.out.println("Failed to create multiple directories!!!");
}
}
}
}
You can follow the link also: roseindia.net/java/beginners/java-create-directory.shtml –
Littlest
© 2022 - 2024 — McMap. All rights reserved.