Java recursively reads all documents under Folder

Source: Internet
Author: User

/*** Recursively read all documents under Folder *@authorAdministrator **/ Public classfileloop{Private StaticList<file> fileList =NewArraylist<file>();  Public Static voidMain (string[] args) {List<File> list = Filereadloop ("C:/dir");  for(inti=0; I<list.size (); i++) {System.out.println (NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"). Format (NewDate (List.get (i). LastModified ()) + ":" +List.get (i). GetName ()); }    }         /*** Loop to get all files under the specified folder *@paramPath*/     Private voidLoopreaddir (String path) {File FilePath=NewFile (path); File[] List=Filepath.listfiles (); if(list!=NULL&& list.length>0){              for(inti=0; i<list.length; i++) {File F=List[i]; if(F.isfile () &&!F.ishidden ())                 {Filelist.add (f); }Else if(F.isdirectory () &&!F.ishidden ())                 {Loopreaddir (F.getpath ()); }             }         }     }          /*** Sort files by date *@paramlist *@return      */     Private voidsortfilelist () {//Sort by file dateCollections.sort (FileList,NewComparator<file>() {@Override Public intCompare (file O1, file O2) {if(O1.lastmodified () >o2.lastmodified ()) {                    return-1; }Else if(o1.lastmodified () = =o2.lastmodified ()) {                    return0; }Else{                    return1;     }            }        }); }     /*** Call static method *@paramPath *@return      */     Public StaticList<file>filereadloop (String path) {Fileloop Filecon=NewFileloop ();        Filecon.loopreaddir (path);        Filecon.sortfilelist (); returnfileList; }}

Java recursively reads all documents under Folder

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.