The delete operation in Java.io.File is useful and common, and can be used to delete individual files and a directory. However, there are times when delete fails, and this can happen for the following reasons:
1, when the deletion of other programs in the use of the file, this will not be deleted correctly
2, no close closed and the file related to all the input and output stream, etc., resulting in the inability to delete (this is the most easy to forget, you may be guilty of this article)
3, when deleting a directory, you must ensure that no other files in the directory can be deleted correctly, or the deletion will fail. You can use Listfiles to process files or directories under the directory.
/*** Delete Files * *@paramfilepathandname * String file path and name such as C:/fqf.txt *@paramfilecontent * String *@returnBoolean*/ Public voiddelfile (String filepathandname) {Try{File Mydelfile=NewFile (filepathandname); System.out.println (Mydelfile.getpath ()); if(!Mydelfile.delete ()) {System.out.println ("Delete Failed"); } } Catch(Exception e) {System.out.println ("Delete file operation Error"); E.printstacktrace (); } }
Java File Delete () execution failure reason