How Java recursively deletes subdirectories and files in a directory

Source: Internet
Author: User

In general, when we save a file in a project, we store the directory information in a single table and store the file information in another table.

When we need to delete the specified directory, we first need to delete all the information in the database directory and subdirectories, then delete the file information, and finally delete the created file directory, if there are subdirectories, you need to use recursive Delete, that is:

Process of deleting subdirectories and files in the specified directory in the business logic layer

Delete directory information from the database Cataloguedao.deletecat (dir);//delete the file information in the database Filedao.deletefile (dir);// Use recursive delete function Deletedir recursively delete the specified directory Deletedir (path + dir.getname ());

Deletedir the implementation of the function, recursively deletes the file under the specified directory:

public static void Deletedir (String path) {File File = new file (path); if (File.exists ()) {if (File.isdirectory ()) {file[] fi Les = File.listfiles (); for (file subfile:files) {if (Subfile.isdirectory ()) Deletedir (Subfile.getpath ()); Elsesubfile.delete ();}} File.delete ();}}


How Java recursively deletes subdirectories and files in a directory

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.