Recursively gets all the filenames, paths, and directories under a directory.
1 Packagetest;2 3 ImportJava.io.File;4 5 Public classgetfilename{6 7 Public Static voidMain (string[] args) {8 //This is the path where the file ' s name want to take.9String Path = "C:\\programdata";Ten getFile (path); One } A - Private Static voidgetFile (String path) { - //get file list where the path has theFile File =NewFile (path); - //Get the Folder List -file[] Array =file.listfiles (); - + if(Array! =NULL)//No access, the error file is null - for(inti = 0; i < Array.Length; i++) { + if(Array[i].isfile ()) {//can be judged in this way if (obj instanceof File) { A //Only take file name atSystem.out.println ("^^ ^^ ^" +array[i].getname ()); - //Take file path and name -System.out.println ("#####" +array[i]); - //Take file path and name -SYSTEM.OUT.PRINTLN ("* * * *" +Array[i].getpath ()); -}Else if(Array[i]instanceofFile) { in GetFile (Array[i].getpath ()); - } to } + } -}
To return these things,
The
Packagetest;ImportJava.io.File;/*** * Get all files in the specified directory (excluding folders), using recursion * *@paramobj *@return */ Public StaticArraylist<file>getlistfiles (Object obj) {File directory=NULL; if(objinstanceofFile) {Directory=(File) obj; } Else{directory=NewFile (obj.tostring ()); } ArrayList<File> files =NewArraylist<file>(); if(Directory.isfile ()) {files.add (directory); returnfiles; } Else if(Directory.isdirectory ()) {file[] Filearr=Directory.listfiles (); for(inti = 0; i < filearr.length; i++) {File Fileone=Filearr[i]; Files.addall (Getlistfiles (Fileone)); } } returnfiles; }
Reference: http://blog.csdn.net/tomorrowzm/article/details/3693653
http://hw1287789687.iteye.com/blog/1946488
java--get all file names under the specified folder