
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to create a directory hierarchy using Java?
The class named File of the java.io package represents a file or directory (path names) in the system. This class provides various methods to perform various operations on files/directories.
The mkdir() method of this class creates a directory with the path represented by the current object.
Creating directory hierarchy
To create a hierarchy of new directories you can using the method mkdirs() of the same class. This method creates the directory with the path represented by the current object, including non-existing parent directories.
Example
import java.io.File; import java.util.Scanner; public class CreateDirectory { public static void main(String args[]) { System.out.println("Enter the path to create a directory: "); Scanner sc = new Scanner(System.in); String path = sc.next(); System.out.println("Enter the name of the desired a directory: "); path = path+sc.next(); //Creating a File object File file = new File(path); //Creating the directory boolean bool = file.mkdirs(); if(bool) { System.out.println("Directory created successfully"); }else { System.out.println("Sorry couldnt create specified directory"); } } }
Output
Enter the path to create a directory: D:\test\myDirectories\ Enter the name of the desired a directory: sample_directory Directory created successfully
If you verify you can observe see the created directory as −
- Related Articles
- How to create a directory using Java?
- How to create a Directory recursively using Java?
- How to create a directory in project folder using Java?
- How to create a directory using Python?
- How to create a Directory using C#?
- How to create a new directory by using File object in Java?
- How to create a directory recursively using Python?
- How to create a unique directory name using Python?
- Create a directory in Java
- How to create a zip archive of a directory using Python?
- How to create a new directory in Linux using the terminal?
- How to list all files in a directory using Java?
- How to list all files (only) from a directory using Java?
- How to create bulk users in the active directory using PowerShell?
- How to read data from all files in a directory using Java?

Advertisements