Open-Source Internship opportunity by OpenGenus for programmers. Apply now.
Reading time: 20 minutes | Coding time: 5 minutes
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.
Use it as follows:
File file = new File(F:\\opengenus_program);
//creating the directory
boolean bool = file.mkdir();
To create a directory:
- 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.
Use it as follows:
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.
Use it as:
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.