This Java example shows how to delete a file or directory using Java. It also shows how to delete a directory recursively.
How to delete a file in Java?
1) Delete a file using java.io.File class
We can use the delete
method of the File class to delete a file.
1 |
public boolean delete() |
This method deletes a file or directory denoted by the path. If the path points to a directory, it must be empty. If the file or directory is deleted, the delete
method returns true. If the file or directory could not be deleted, it returns false.
Please note that the delete
method may throw the java.lang.SecurityException if delete access is denied.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.javacodeexamples.ioexamples; import java.io.File; public class DeleteFileExample { public static void main(String[] args){ file = new File("c:/dir1/example.txt"); isFileDeleted = file.delete(); System.out.println("Is file deleted: " + isFileDeleted); } } |
How to delete a file when the Virtual Machine exits?
To delete a file when the virtual machine is terminated, use the deleteOnExit
method.
1 |
public void deleteOnExit() |
This method requests the file represented by the path to be deleted when the virtual machine is terminated.
2) Delete a file using the Apache Commons IO FileUtils
If you are using the Apache Commons IO library, use the deleteQuietly
method of the FileUtils class.
1 |
public static boolean deleteQuietly(File file) |
This method deletes the file and returns true if the file is deleted successfully. It does not throw any exception. When the file cannot be deleted, it returns false.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.javacodeexamples.ioexamples; import java.io.File; import org.apache.commons.io.FileUtils; public class DeleteFileExample { public static void main(String[] args){ File file = new File("c:/dir1/example.txt"); boolean isFileDeleted = FileUtils.deleteQuietly(file); System.out.println("Is file deleted: " + isFileDeleted); } } |
Warning: If the file denoted by the path is a directory, the deleteQuietly
method will delete it recursively.
How to delete a directory in Java?
1) Delete directory using java.io.File class
Use the delete
method of the File class to delete a directory.
1 |
public boolean delete() |
This method returns true if the directory is deleted, false otherwise.
1 2 3 4 |
File directory = new File("c:/dir1/"); boolean isDirDeleted = directory.delete(); System.out.println("Is directory deleted: " + isDirDeleted); |
Note: The directory must be empty before we can delete it. If the directory is not empty, the delete
method does not delete it and returns false.
How to delete a directory recursively?
To delete a directory recursively including all its sub-directories and files, use the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
package com.javacodeexamples.ioexamples; import java.io.File; import java.io.IOException; public class DeleteDirectoryExample { public static void main(String[] args)throws IOException{ File directory = new File("c:/dir1"); deleteRecursive(directory); System.out.println("Directory is deleted recursively"); } static void deleteRecursive(File file) throws IOException{ if( file.isDirectory() ){ for(File f : file.listFiles()){ deleteRecursive(f); } } if(!file.delete()){ throw new IOException("File cannot be deleted: " + file); } } } |
2) Delete directory using the Apache Commons IO FileUitls class
Use the deleteDirectory
method of the FileUtils class to delete a directory recursively.
1 |
static void deleteDirectory(File directory) |
Here is the example code to delete a directory recursively.
1 2 |
File directory = new File("c:/dir1"); FileUtils.deleteDirectory(directory); |
You can also use the deleteQuietly
method to delete a directory recursively.
1 2 |
File directory = new File("c:/dir1"); FileUtils. deleteQuietly(directory); |
This example is a part of the Java File class tutorial with examples.
Please let me know your views in the comments section below.