Gopal00005
Gopal00005

Reputation: 2151

How to create a directory and sub directory structure with java?

Hello there I want to create the directories and sub directories with the java. My directory structure is starts from the current application directory, Means in current projects directory which looks like following...

Images
   |
   |+ Background
   |
   |+ Foreground
          |
          |+Necklace
          |+Earrings
          |+Etc...

I know how to create directory but I need to create sub directory I tried with following code what should be next steps ?

File file = new File("Images");
file.mkdir();

Upvotes: 30

Views: 101628

Answers (6)

Adding up to @ROMANIA_engineer's answer..

I used Path.resolve() method to create sub-directories using variables lika DateTime and others:

private final Path root = Paths.get("target\\");
private final Path batchFilePath = Paths.get(root.resolve(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyymmdd_HHmmss"))).toString());

Files.createDirectory(root);
Files.createDirectory(batchFilePath);

And the result was: directory/subdirectory example

Upvotes: 0

Chandrasekhar Rajoli
Chandrasekhar Rajoli

Reputation: 158

You can create all parent directories by using File.mkdirs().

File.mkdirs() - Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories. Note that if this operation fails it may have succeeded in creating some of the necessary parent directories.

Upvotes: 7

Rupesh tiwari
Rupesh tiwari

Reputation: 41

You can just use file.mkdirs(), it will create sub-directory.

String path = images + File.separator + Background + File.separator + Foreground + File.separator + Necklace + File.separator  + Earrings ;
File file = new File( path );
file.mkdirs();

Upvotes: 4

ROMANIA_engineer
ROMANIA_engineer

Reputation: 56666

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);

Upvotes: 29

Michael Aaron Safyan
Michael Aaron Safyan

Reputation: 95519

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.

Upvotes: 52

Elliott Frisch
Elliott Frisch

Reputation: 201447

You could do it with File#mkdirs() and something like,

// The "/" is cross-platform safe as a path-separator in Java.
// So is "\\" but that's twice the characters!
String path = createImages.getAbsolutePath() + "/Images";
File f = new File(path);
if (!f.isDirectory()) {
  boolean success = f.mkdirs();
  if (success) {
    System.out.println("Created path: " + f.getPath());
  } else {
    System.out.println("Could not create path: " + f.getPath());
  }
} else {
  System.out.println("Path exists: " + f.getPath());
}

Per the linked Javadoc,

Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories. Note that if this operation fails it may have succeeded in creating some of the necessary parent directories.

Upvotes: 4

Related Questions