Java implementation delete files and folders

Source: Internet
Author: User

First, it needs to be clear that the delete () method in the file class applies to deleting an empty directory, or a single file.

Then, the deletion of folders above the level two directory needs to be removed in two steps.

1. Delete the file below the bottom directory, or empty directory

When there are multiple files, you need to loop to delete the following code:

New File (scriptdir); if (File_scriptdir.exists () | | file_scriptdir.isdirectory ()) {     = file_scriptdir.list ();     if (children.length>0) {          for (int i=0;i<children.length;i++) {                 New  File (file_scriptdir,children[i]). Delete ();}       }}

2. Delete the parent directory, and the directory is empty

When you delete an empty directory, you can use the Delete () method directly.

Finally, if you are running in a Linux environment, you can use the command line, but there are also requirements for folders.

Java implementation delete files and folders

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.