Java File delete() Method
Example
Create a file, then delete it:
import java.io.File;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
File myFile = new File("filename.txt");
myFile.createNewFile();
System.out.println(myFile.delete());
System.out.println(myFile.exists());
}
}
Definition and Usage
The delete() method deletes a file or a directory (folder).
A directory must be empty before it can be deleted. If the file does not exist, nothing happens and the method returns false.
Tip: Use the exists() method to check if a file is still there.
Syntax
public boolean delete()
Parameter Values
None.
Technical Details
| Returns: | true if the file or directory was deleted, otherwise false. |
|---|
Related Pages
❮ File Methods