How to delete a file in Java?

To delete a file in Java, call the File.delete method.

For example, this is how you can delete image.png file from the /Users/whaadev directory:

File file = new File("/Users/whaadev/image.png");
file.delete();