- How to create a directory in project folder using Java?
- Example
- Output
- How to create a new directory by using File object in Java?
- Creating a new directory
- Example
- Output
- Creating directory hierarchy
- Example
- Output
- Creating folder with java
- Learn Latest Tutorials
- Preparation
- Trending Technologies
- B.Tech / MCA
- Javatpoint Services
- Training For College Campus
- Creating and Deleting Folders in Java
- Creating a folder
- Creating folder hierarchy
- Deleting an empty Folder
- Deleting non empty Folders Recursively
- Kushagra Garg
- Software Engineering
- Try these questions if you think you know Linear Search
- Inner Class in Java
How to create a directory in project folder 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.
Therefore, to create a directory −
- Instantiate the File class by passing the path of the directory you need to create, as a parameter (String).
- Invoke the mkdir() method using the above-created file object.
Example
The following Java example reads the path and name of the directory to be created, from the user, and creates it.
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.mkdir(); if(bool)< System.out.println("Directory created successfully"); >else < System.out.println("Sorry couldn’t create specified directory"); >> >
Output
Enter the path to create a directory: D:\ Enter the name of the desired a directory: sample_directory Directory created successfully
If you verify, you can observe see the created directory as −
But, if you specify a path in a drive that doesn’t exist, this method will not create the required directory.
For example, if the D drive of my (windows) system is empty and if I specify the path of the directory to be created as −
D:\test\myDirectories\sample_directory
Where the test and myDirectories folders don’t exist, the mkdir() method will not create it.
How to create a new directory by using File object in 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.
Creating a new directory
The mkdir() method of this class creates a directory with the path represented by the current object.
Therefore, to create a directory −
- Instantiate the File class by passing the path of the directory you need to create, as a parameter (String).
- Invoke the mkdir() method using the above created file object.
Example
Following Java example reads the path and name of the directory to be created, from the user, and creates it.
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.mkdir(); if(bool)< System.out.println("Directory created successfully"); >else < System.out.println("Sorry couldn’t create specified directory"); >> >
Output
Enter the path to create a directory: D:\ Enter the name of the desired a directory: sample_directory Directory created successfully
If you verify, you can observe see the created directory as −
But, if you specify a path in a drive that doesn’t exist, this method will not create the required directory.
For example, if the D drive of my (windows) system is empty and if I specify the path of the directory to be created as −
D:\test\myDirectories\sample_directory
Where the test and myDirectories folders doesn’t exist, the mkdir() method will not create it.
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 −
Creating folder with java
Learn Latest Tutorials
Preparation
Trending Technologies
B.Tech / MCA
Javatpoint Services
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
- Website Designing
- Website Development
- Java Development
- PHP Development
- WordPress
- Graphic Designing
- Logo
- Digital Marketing
- On Page and Off Page SEO
- PPC
- Content Development
- Corporate Training
- Classroom and Online Training
- Data Entry
Training For College Campus
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week
Like/Subscribe us for latest updates or newsletter
Creating and Deleting Folders in Java
In this article at OpenGenus, we are going to discuss how to create and delete folders in Java. Folders are called directories in Java. There are various ways for creating and deleting a directory. We will see them one by one.
File Java class of java.io package represents a file or directory in the system. It contains various methods to perform operations on file/ directory.
Creating a folder
mkdir() method of file class is used to create a new directory denoted by the abstract pathname. It returns boolean true or false , if the directory gets created or not respectively.
File file = new File(F:\\opengenus_program); //creating the directory boolean bool = file.mkdir();
- Create the object of the File class by passing the path of the directory, as a parameter (String) to the constructor of the File class
- call the mkdir() method using the above created file object
Example of Java code to create a directory using mkdir() method.
import java.io.File; public class CreateDirectory< public static void main(String args[])< //creating a File object File file = new File(F:\\program); //creating the directory boolean bool = file.mkdir(); if(bool)< System.out.println("Directory created successfully"); >else < System.out.println("Directory not created"); >> >
Creating folder hierarchy
mkdirs() method of file class is used to create the directory named by this abstract pathname, including any necessary but nonexistent parent directories. It returns boolean true if and only if the directory was created, along with all necessary parent directories; false otherwise.
Note: If this operation fails it may have succeeded in creating some of the necessary parent directories.
File file = new File(opengenus_path); //Creating the directory boolean bool = file.mkdirs();
Example of java code to create the folder hierarchy using mkdirs() method.
import java.io.File; public class CreateDirectory < public static void main(String args[]) < //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("Directory not created"); >> >
Deleting an empty Folder
delete() method of the file class is used to delete an empty directory. It returns false if the directory is not present.
File file = new File("F:\\opengenus\file"); boolean bool = file.delete(); // works only if directory is empty
Example of Java code to delete a folder.
import java.io.File; public class JavaDeleteDirectory < public static void main(String[] args)< File file = new File("F:\\program"); boolean bool = file.delete();//works only if directory is empty if(bool)< System.out.println("Directory deleted successfully"); >else < System.out.println("Directory not deleted"); >> >
Deleting non empty Folders Recursively
In Java, we cannot delete folder that are not empty. The workaround is to delete all files first using delete() utility and then, recursively delete empty folders using delete() utility starting from the inner most folders.
delete() function can be used recursively to delete non-empty folders/ directory. We can define a recursive function named recursiveDelete() which will:
- check if the current location is a file or folder
- If it is a file, then it will be deleted
- If it is an empty folder, it will be deleted
- If it is not an empty folder, it will go through all objects within the folder and apply the function recursively
Follow this function carefully:
public static void recursiveDelete(File file) < //to end the recursive loop if (!file.exists()) return; //if directory, go inside and call recursively if (file.isDirectory()) < for (File f : file.listFiles()) < //call recursively recursiveDelete(f); >> //call delete to delete files and empty directory file.delete(); >
Example of Java code to delete a folder recursively
import java.io.File; public class DeleteFolderRecursively < public static void main(String[] args) < String folder = "F:\\program"; //delete folder recursively recursiveDelete(new File(folder)); >public static void recursiveDelete(File file) < //to end the recursive loop if (!file.exists()) return; //if directory, go inside and call recursively if (file.isDirectory()) < for (File f : file.listFiles()) < //call recursively recursiveDelete(f); >> //call delete to delete files and empty directory file.delete(); System.out.println("Deleted file/folder: "+file.getAbsolutePath()); > >
With this article at OpenGenus, you have the complete knowledge of creating and deleting folders in Java using the io package. Enjoy.
Kushagra Garg
Computer Science undergraduate at Vellore Institute of Technology (VIT) (2017 to till date)
OpenGenus Tech Review Team
Software Engineering
Try these questions if you think you know Linear Search
Must attempt questions on Linear Search algorithm. Linear Search seem to be a simple algorithm but understanding it deeply requires expertise. It is a guarantee that you will learn new things about this on going through our questions.
OpenGenus Tech Review Team
Inner Class in Java
In Java, inner class is a nested class which is defined within another class and it provides better encapsulation. There are several variants like anonymous inner class, method local inner class and much more.
Shujaa Ahmad