You can use File.mkdir() or File.mkdirs() to create a directory. Between the two, the latter method is more tolerant and will create all intermediate directories as needed. Also, since I see that you use "\\" in your question, I would suggest using File.separator for a portable path separator string.
Answer from Michael Aaron Safyan on Stack OverflowYou can use File.mkdir() or File.mkdirs() to create a directory. Between the two, the latter method is more tolerant and will create all intermediate directories as needed. Also, since I see that you use "\\" in your question, I would suggest using File.separator for a portable path separator string.
Starting from Java 7, you can use the java.nio.file.Files & java.nio.file.Paths classes.
Path path = Paths.get("C:\\Images\\Background\\..\\Foreground\\Necklace\\..\\Earrings\\..\\Etc");
try {
Files.createDirectories(path);
} catch (IOException e) {
System.err.println("Cannot create directories - " + e);
}
This is a tricky solution (because I used only one path to go to the whole structure).
If you don't like tricky solutions, you can use 4 simple paths instead:
Path p1 = Paths.get("C:\\Images\\Background");
Path p2 = Paths.get("C:\\Images\\Foreground\\Necklace");
Path p3 = Paths.get("C:\\Images\\Foreground\\Earrings");
Path p4 = Paths.get("C:\\Images\\Foreground\\Etc");
and then call the createDirectories method for all of them:
Files.createDirectories(p1);
Files.createDirectories(p2);
Files.createDirectories(p3);
Files.createDirectories(p4);
Videos
Not sure if such a method exists, but you can certainly define one:
import java.io.File;
import java.util.Arrays;
class Test {
public static boolean createDirectoriesWithCommonParent(
File parent, String...subs) {
parent.mkdirs();
if (!parent.exists() || !parent.isDirectory()) {
return false;
}
for (String sub : subs) {
File subFile = new File(parent, sub);
subFile.mkdir();
if (!subFile.exists() || !subFile.isDirectory()) {
return false;
}
}
return true;
}
public static void main(String[] args) {
createDirectoriesWithCommonParent(new File("test/foo"), "a", "b", "c");
}
}
We can create a directory or multiple directories Using Path in Simple step:
public static Path createDirectories() throws IOException {
String folderPath = "E://temp/user/UserId/profilePicture";
Path path = Paths.get(folderPath);
return Files.createDirectories(path);
}
Path, Paths and Files classes can be found java.nio.file.*; package which is given below :
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Note :- Make sure method should throws IOException or within try-catch.
Use this:
File targetFile = new File("foo/bar/phleem.css");
File parent = targetFile.getParentFile();
if (parent != null && !parent.exists() && !parent.mkdirs()) {
throw new IllegalStateException("Couldn't create dir: " + parent);
}
While you can just do file.getParentFile().mkdirs() without checking the result, it's considered a best practice to check for the return value of the operation. Hence the check for an existing directory first and then the check for successful creation (if it didn't exist yet).
Also, if the path doesn't include any parent directory, parent would be null. Check it for robustness.
Reference:
- File.getParentFile()
- File.exists()
- File.mkdir()
- File.mkdirs()
You can use Google's guava library to do it in a couple of lines with Files class:
Files.createParentDirs(file);
Files.touch(file);
https://code.google.com/p/guava-libraries/
new File("/path/directory").mkdirs();
Here "directory" is the name of the directory you want to create/exist.
After ~7 year, I will update it to better approach which is suggested by Bozho.
File theDir = new File("/path/directory");
if (!theDir.exists()){
theDir.mkdirs();
}
This is sample program to make directories in java. You can take it as reference and make program to create directories with name current year, current month, current day.
- First take current date using java.util.Date
- Then perform some string operation in date to get names of directories.
And do some changes in this code as per your need.
// shows how to create multiple directories in java // (multiple directory levels) public class JavaCreateMultipleDirectoriesExample { public static void main(String[] args) { // the folders "000/111/222" don't exist initially File dir = new File("/Users/al/tmp/000/111/222"); // create multiple directories at one time boolean successful = dir.mkdirs(); if (successful) { // created the directories successfully System.out.println("directories were created successfully"); } else { // something failed trying to create the directories System.out.println("failed trying to create the directories"); } } }
I made it by making different directories each time.
String pathname1 = "C:";
String db = "db";
pathname1 += find2 + "/" + db;
System.out.println(pathname1);
File directory1 = new File(pathname1);
if (!directory1.exists()) {
directory1.mkdirs();
}
String pathname2 = "C:";
String ocr = "ocr";
pathname2 += find2 + "/" + ocr;
System.out.println(pathname2);
File directory2 = new File(pathname2);
if (!directory2.exists()) {
directory2.mkdirs();
}
String pathname3 = "C:";
String output = "output";
pathname3 += find2 + "/" + output;
System.out.println(pathname3);
File directory3 = new File(pathname3);
if (!directory3.exists()) {
directory3.mkdirs();
}
String pathname4 = "C:";
String scans = "scans";
pathname4 += find2 + "/" + scans;
System.out.println(pathname4);
File directory4 = new File(pathname4);
if (!directory4.exists()) {
directory4.mkdirs();
}