How to create a directory and subdirectory structure using java?

Hello, I want to create directories and subdirectories using java. My directory structure starts with the current application directory, in the current project directory, which looks like this ...

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

I know how to create a directory, but I need to create a subdirectory that I tried with the following code, what should be the next step?

 File file = new File("Images"); file.mkdir(); 
+12
java
source share
6 answers

You can use File.mkdir () or File.mkdirs () to create a directory. Between them, the latter method is more tolerant and will create all intermediate directories as necessary. Also, since I see that you are using "\\" in your question, I would suggest using File.separator for a portable path separator string.

+27
source share

Starting with Java 7 , you can use java.nio.file.Files and java.nio.file.Paths .

 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 difficult decision (because I used only one way to go to the whole structure).

If you don't like complex solutions, you can use 4 simple ways:

 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); 
+16
source share

You can create all parent directories with File.mkdirs () .

File.mkdirs () - Creates a directory called this abstract path, including any necessary but non-existent parent directories. Please note that if this operation fails, she may have been able to create some of the required parent directories.

+4
source share

You can do this with File#mkdirs() and something like

 // The "/" is cross-platform safe as a path-separator in Java. // So is "\\" but that 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()); } 

In related javadoc,

Creates a directory with the name of this abstract path, including any necessary but non-existent parent directories. Please note that if this operation failed, it may have been able to create some of the required parent directories.

+3
source share

To create a directory in Java using the code below.

  • for one directory that we use.

new file ("D: \\ Images"). mkdir ();

  1. for subdirectories

new file ("D: \\ Images \\ Foreground \\ Earrings"). mkdirs ();

For example:

 package com.msr.io; import java.io.File; public class CreateDirectory { public static void main(String[] args) { File file = new File("D:\\Images"); if (!file.exists()) { if (file.mkdir()) { System.out.println("directory created successfully"); } else { System.out.println("directory is not created"); } } File files = new File("D:\\Images\\Foreground\\Earrings"); if (!files.exists()) { if (files.mkdirs()) { System.out.println("sub directories created successfully"); } else { System.out.println("failed to create sub directories"); } } } } 
+1
source share

You can just use file.mkdirs() , it will create a subdirectory.

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

All Articles