A way for Java to recursively delete a directory

Source: Internet
Author: User

1  Public Static voidDeldir (File f) {2     //determine if it is a directory, not skip it, delete it directly, or, if it is a directory, clear its contents first.3     if(F.isdirectory ()) {4         //get sub File/directory5file[] Subfiles =f.listfiles ();6         //traverse the directory7          for(File subfile:subfiles) {8             //recursive call to delete the file: If this is an empty directory or file, it can be deleted once recursively. If this is a non-empty directory, multiple9             //recursively empties its contents before deletingTen Deldir (subfile); One         } A     } -     //Delete an empty directory or file - F.delete (); the}

By the way, a 64-bit system can probably call tens of thousands of recursion. We don't basically create directories with more than 10,000 layers. So this method can deal with the general situation, without causing problems such as memory overflow.

A way for Java to recursively delete a directory

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.