A file and directory in java can be deleted by using delete
method on java.io.File
object which points to the underlying physical file or directory.
delete
method will delete a directory only if it is empty otherwise it would not remove the directory.
This post will discuss different methods to delete a non-empty directory in java.
Create a
java.io.File
object which points to the directory to be deleted. Get a list of all files in this directory using listFiles
method.This method returns an array of
java.io.File
objects each of which represents a file in the directory.Iterate over the files and in every iteration delete a file. Example,
import java.io.File;
public class DeleteDirectoryExample {
public static void main(String[] args) {
String directoryPath = "E:/dummyfolder";
// create a file object for directory
File directory = new File(directoryPath);
// get all files
File[] files = directory.listFiles();
// iterate over files
for (File file : files) {
// delete each file
file.delete();
}
// delete empty directory
directory.delete();
}
}
Above method will delete files in a directory till a single level, that is, files that reside directly under the given directory.
If there are sub-folders inside the given directory, then they will not be deleted. For this, use the methods listed below.
This method can delete files in the given directory as well as files inside nested folders also. It iterates over the files in the given directory using a loop.
In every iteration, the method checks if the current file object represents a directory or a file. If it is a file, then it is deleted.
If it is a directory, then this method calls itself recursively and iterates over the files in this directory. It continues for nested sub-folders and other folders as well.
import java.io.File;
public class DeleteFileRecursiveExample {
public static void main(String[] args) {
String directoryPath = "E:/Test";
// create a file object for main directory
File directory = new File(directoryPath);
// call recursive method
deleteFile(directory);
}
/**
* Recursive method to delete directory structure
* @param file
*/
static void deleteFile(File file) {
// get all files
File[] files = file.listFiles();
// iterate over files
for (File nestedFile : files) {
// check for sub-directory
if (nestedFile.isDirectory()) {
// call this method again
deleteFile(nestedFile);
} else {
// delete each file
nestedFile.delete();
}
}
// check if the directory is empty, then delete it
if (file.listFiles().length == 0) {
file.delete();
}
}
}
Apache Commons Library has a class
org.apache.commons.io.FileUtils
class for performing file related operations.This class has a
deleteDirectory
method which accepts an argument of type java.io.File
representing the directory to be deleted and deletes this directory along with its contents.This method can also be used to delete a complete folder structure consisting of multiple sub-folders and files.
import java.io.File;
import org.apache.commons.io.FileUtils;
public class DeleteFileRecursiveExample {
public static void main(String[] args) throws IOException {
String directoryPath = "E:/Test";
// create a file object for main directory
File directory = new File(directoryPath);
FileUtils.deleteDirectory(directory);
}
}
deleteDirectory
method throws a java.io.IOException
. You need to throw it or handle it.
Method 4: Using nio java 7
Java 7 introduced java.nio.file.Files
class for performing file related operations. This class has a walkFileTree
method which accepts below objects as arguments
- java.nio.file.Path: A path object representing the directory.
- java.nio.file.SimpleFileVisitor: A visitor object with methods that are automatically called for each file and directory.
SimpleFileVisitor
class has a method visitFile
which is called for each file in the directory or sub-directory and a method postVisitDirectory
which is called for every directory.
Both these methods accept a path object as argument representing the file or directory. Thus, visitFile
may be used to delete files and postVisitDirectory
may be used to delete a directory. Example,
import java.io.File;
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 DirectoryDeleteExample {
public static void main(String[] args) throws IOException {
String directoryPath = "E:/Test";
// create a file object for main directory
File directory = new File(directoryPath);
Path path = Paths.get(directoryPath);
// iterate over the directory
Files.walkFileTree(path, new SimpleFileVisitor() {
// called for each file in the directory
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
// delete file
Files.delete(file);
return FileVisitResult.CONTINUE;
}
// called for each directory
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc)
throws IOException {
// delete directory
Files.delete(dir);
return FileVisitResult.CONTINUE;
}
});
}
}
In the above method, we override(Notice the @Override annotation) the visitFile
and postVisitDirectory
methods of SimpleFileVisitor
as per our requirement.
This method can also be used to delete a directory that contains sub-folders as well.
Hit the clap if the post was helpful.