Java Delete files or directories and all files in the directory

Source: Internet
Author: User

has been doing the C + + related development work. Suddenly one day on the rise, want to learn Java development. And then find a simple Java tutorial online, Getting started is enough. See the file IO Chapter, recall the previous use of C + + to delete files or directories of the exercise, so I intend to use Java to implement this function. This thought is very simple function, still encountered some problems. Carefully analyze the code and navigate to the problem area. Record here and remind yourself not to repeat it.

1. The first version of the delete function is written like this

1 Static Booleandelfile (String filename) {2File File =NewFile (filename);3         if(!file.exists ()) {4             return false;5         }6 7         if(File.isfile ()) {8             returnFile.delete ();9}Else {TenString[] Filenames =file.list (); One              for(String f:filenames) { A delfile (f); -             } -             returnFile.delete (); the         } -}

The code can be compiled, but the results do not match the expected results. If the problem arises in a large system, it is often a very small headache. My approach is to meet their unfamiliar, first build a small project to achieve this function, add test cases. If the test passes, it will be ported to the big system.

After careful analysis, the above code problem is in line 10th, file.list () Returns the result is the file name of all files under the current path, and does not include the path. So the recursive call is returned because the detection file does not exist. Then there is the following version.

2. Correct deletion of file or directory methods

1 Static Booleandelfile (file file) {2         if(!file.exists ()) {3             return false;4         }5 6         if(File.isfile ()) {7             returnFile.delete ();8}Else {9file[] Files =file.listfiles ();Ten              for(File f:files) { One delfile (f); A             } -             returnFile.delete (); -         } the}

This version of the code directly takes the file object as a parameter, and File.listfiles () returns a collection of file objects that are also self-files. The file object holds the exact location of the files, so there is no problem with the previous version, and this version works correctly.

While the code works, it is not the most concise version. The following is a more concise version.

3. Simple and functional version

1 Static Booleandelfile (file file) {2         if(!file.exists ()) {3             return false;4         }5 6         if(File.isdirectory ()) {7file[] Files =file.listfiles ();8              for(File f:files) {9 delfile (f);Ten             } One         } A         returnFile.delete (); -}

The same can work, but the code is not duplicated.

Java Delete files or directories and all files in the 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.