Hi everyone!! Welcome to flower brackets blog. In this post you are going to learn java delete directory.
Using delete method we can delete directory java. But the directory should be empty in order to delete it.
Example: Java Delete Directory
Also Read – Create Directory Java Program
import java.io.*; public class DeleteDirectory { public static void main(String[] args) { File fl = new File("C://DeleteDirectory/demo_delete.txt"); boolean blDelete = fl.delete(); System.out.println("To check the file deleted True/False: " + blDelete); } }
Output:
To check the file deleted True/False: true
recursive delete
Here we will see java program to delete files in a directory. If the directory contains a lot of subdirectories and files then we have to use recursive delete in order to delete all subdirectories and files.
Let’s see an example on how to delete all files in a directory java,
import java.io.File; import java.io.IOException; public class DeleteDirectory { private static final String folder = "D:/project/java"; public static void main(String[] args) throws IOException { File fl = new File(folder); if(!fl.exists()) // checking if directory exists { System.out.println("Sorry!! directory doesn't exist."); } else { DeleteDirectory dd = new DeleteDirectory(); dd.deleteDirectory(fl); } } public void deleteDirectory(File file) throws IOException { if(file.isDirectory()) { if(file.list().length == 0) { deleteEmptyDirectory(file); // here if directory is empty delete we are deleting } else { File fe[] = file.listFiles(); for(File deleteFile : fe) { deleteDirectory(deleteFile); // recursive call } if(file.list().length == 0) { deleteEmptyDirectory(file); } } } else { file.delete(); System.out.println("File deleted : " + file.getAbsolutePath()); } } private void deleteEmptyDirectory(File fi) { fi.delete(); System.out.println("Directory deleted : " + fi.getAbsolutePath()); } }
deleting directory and its children
Here we are going to learn delete all files in a directory java,
import java.io.File; import java.io.IOException; public class DeleteDirectoryDemo { public static void main(String[] args) { String dirPath = "D:/project/java"; File fl = new File(dirPath); try { deleteDirectory(fl); System.out.println("Directory deleted!"); } catch(IOException ioe) { System.out.println("Error detected : " + dirPath); ioe.printStackTrace(); } } private static void deleteDirectory(File file) throws IOException { for (File chFile : file.listFiles()) { if (chFile.isDirectory()) { deleteDirectory(chFile); } else { if (!chFile.delete()) { throw new IOException(); } } } if (!file.delete()) { throw new IOException(); } } }
using walkfiletree
Now let’s learn delete all files in a directory java using walkfiletree method which navigates in the given folder. Here’s the code,
import java.io.IOException; import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; public class DeleteDirectoryExample { public static void main(String[] args) { String dirPath = "D:/project/java"; try { // Deleting the directory recursive method deleteDirectory(dirPath); System.out.println("Directory deleted!!!"); } catch (IOException ioe) { System.out.println("Error detected : " + dirPath); ioe.printStackTrace(); } } private static void deleteDirectory(String dirName) throws IOException { Path directory = Paths.get(dirName); Files.walkFileTree(directory, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file,BasicFileAttributes attrs) throws IOException { Files.delete(file); return FileVisitResult.CONTINUE; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc)throws IOException { Files.delete(dir); return FileVisitResult.CONTINUE; } }); } }
conclusion
That’s it guys. This is all about java delete directory in java. I hope you have understood the concept.
You can subscribe to my blog flower brackets if you haven’t already.
Do share this article if you like.