Learn to delete a specified file or directory in Java. Note that different methods behave differently for deleting non-empty directories.
1. Deleting with File Class
To delete a file, File class provides the following methods:
- It deletes the specified file or directory. In the case of a directory, the directory must be empty in order to be deleted.
- This method returns
trueif and only if the file or directory is successfully deleted;
- In case of any permission issues,
- In the file cannot be deleted for any reason then it does not throw any exception, rather it simply returns
- It registers the file for deletion when the virtual machine terminates.
- It is useful in the case of unit testing to delete temporary files after the test execution is finished.
- Note that once deletion has been requested, it is not possible to cancel the request.
- Deletion will be attempted only when the JVM terminates normally, otherwise, the behavior is unspecified.
- If a file or directory is already for deletion then this method has no effect.
//Deleting a file immidiately File file = new File("c:/temp/one.txt"); boolean deleted = file.delete(); //Registering for deletion File file = new File("c:/temp/two.txt"); file.deleteOnExit();
2. Deleting with java.nio.file.Files
Files class also provides two following methods:
- Similar to File.delete(), this method also deletes a file or an empty directory.
- The difference is that this method throws
IOExceptionif the file cannot be deleted which is useful in debugging the reason for failure.
- It throws
NoSuchFileExceptionif the specified file or directory does not exist.
- Similarly, it throws
DirectoryNotEmptyExceptionif the specified directory is not empty.
- This method is a little different version of delete(). It does not throw
NoSuchFileExceptionif the file or directory is not present.
- It deletes a file or directory if it exists.
- This method returns true if the file was deleted by this method; false if the file could not be deleted.
Path path = Path.of("c:/temp/one.txt"); Files.delete(path); //or Path path = Path.of("c:/temp/two.txt"); boolean success = Files.deleteIfExists(path);
3. Deleting with Commons IO’s FileUtils
The FileUtils class has following useful methods for deleting the files and directories:
File delete(file) : deletes a file or directory. Internally it uses Files.delete() method.
void deleteDirectory(file) : deletes a directory recursively. It returns IOException in case the deletion is unsuccessful.
boolean deleteQuietly(file) : deletes a file without ever throwing an exception. If the file is a directory, delete it and all sub-directories. It does not require the directory to be empty as it is needed with other methods.
FileUtils.delete(file); FileUtils.deleteQuietly(new File("c:/temp")); boolean success = FileUtils.deleteDirectory(new File("c:/temp"));
Deleting a file or directory in Java is a very simple operation and mostly done in a single statement. Still, it may fail sometimes for two reasons i.e. permission issues and a non-empty directory.
As a best practice, we can use Files.delete(path) for deleting a file and FileUtils.deleteDirectory() for deleting a directory recursively.
Happy Learning !!