/*** 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