This example will demonstrate how to delete a file using java, java 7 NIO and apache commons. In the set up we will create a file for each of the snippets to delete.
Straight up Java
Using File.delete will delete a file or directory by the path defined. In the instance the directory is not empty it won't be deleted. The file.delete() will return a boolean value to indicate if the delete operation was successful. True if the file was deleted, false if it was not. You could also check if the file exists by calling fileToDelete.exists().
Java 7 File I/O
This snippet will show how to delete a file using java 7 NIO and Files utility method. We will first use Paths to obtain a reference to the location of the file and pass it to the Files.delete method. A key difference between this method and the File.delete above is that it will not return if it was successful. Be aware that on certain file systems the file must be closed to delete it.
Using apache commons we will call FileUtils.deleteQuietly to delete a file in java. This approach will not throw an exception and if it is a directory it will be delete with all of it's sub directories.