1. Introduction
The delete() method is a part of a Java I/O API. This method deletes the file or directory but assuming the directory is empty. Every time operation cannot be executed the IOException
has been thrown and this applies to every method from java.io.File
class.
2. Method signature
Parameters:
- method does not take any parameter
Returns
- true if and only if the file or directory is successfully deleted
Throws
- SecurityException when access to the file or directory is denied
3. Examples
3.1. Delete an existing file
The output:
3.2. Delete an empty directory
The output:
Trying to delete the directory that does not exist will give the following result:
Trying to delete a directory that is not empty will give the following output:
4. Conclusion
In this article we presented the File.delete() the method from Java I/O API. We used several examples to show how to use it and what results we should expect in different scenarios.
{{ 'Comments (%count%)' | trans {count:count} }}
{{ 'Comments are closed.' | trans }}