Java recursive implementation deletes or queries all files under the specified directory

Source: Internet
Author: User

/** * Recursively lists the names of all files under the drive letter, such as E:\HeartIsland *  * @author Heartisland *  */public class Filelistdemo {    /**     * Recursively enumerates the names of all the files under the drive letter. If all the files under E:\HeartIsland are printed out, the path prints     *     * File File = new file ("e:\\heartisland\\");     *  //List The path of the file     *  System.out.println (File.getpath ());     *  //Inferred whether it is a directory     *  System.out.println (file.isdirectory ());     *  //List A file or directory below a drive letter * */public    static void Test (file file) {        file[] files = file.listfiles () ;        for (int i = 0; i < files.length; i++) {            if (files[i].isdirectory ()) {                test (files[i]);            } else {                Sys Tem.out.print ("Delete succeeded:" +files[i].getpath () + "\ T");//file path query and print                System.out.print ("Run:" +files[i].delete () + "\ n ")///File run Delete (with return value)}}} public    static void Main (string[] args) {        file File = new file (" d:\\ Heartisland\\ ");        Test (file);}    }





Read permission class for file class:


public boolean canexecute ();//inference can be run enough


public boolean canRead ();//Infer whether the file is readable enough


public boolean canWrite ();//Infer whether the file is writable




public boolean createnewfile ();//Create a new file


public boolean Delete ()//delete file or directory


Public String GetAbsolutePath ();//Get the absolute path to the file


Public String getName ();//Get the name of the file


public string GetPath ();//change path name to string




public boolean isdirectory ();//Infer whether the file is a directory


public boolean isfile ();//Infer whether the file is a file


public boolean Ishidden ();//Infer whether the file is hidden


public long length ();//Returns the length of the file

Java recursive implementation deletes or queries all files under the specified 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.