Java File Delete () execution failure reason

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.