Package A.ab;import Java.io.file;import Java.io.ioexception;public class AE {public static void main (string[] args) {File F=new File ("D:\\dd"); New AE (). FileList (f);} Public void FileList (file fl) {try{file[] Fs=fl.listfiles (), for (file File:fs) {if (File.isdirectory ()) { System.out.println (File.getname ()); FileList (file);} Else{system.out.println (File.getname ());}}} catch (Exception e) {e.printstacktrace ();}}}
Listfiles
Listfiles ()
-
Returns an abstract path to the an array group that represents the files in the directory represented by this abstract path name.
If this abstract path name does not represent a directory, this method returns null
. Otherwise an File
array of objects is returned, with each array element corresponding to each file or directory in the directory. Indicates that the name of the directory itself and its parent directory are not included in the results. Each abstract path name that is obtained is constructed using the constructor method, based on this abstract pathname File(File, String)
. Therefore, if this pathname is an absolute pathname, then each path name obtained is an absolute pathname, and if the pathname is a relative pathname, each path name obtained is relative to the same directory's pathname.
It is not guaranteed that the same strings in the resulting array will appear in a particular order, especially if they are not guaranteed to appear in alphabetical order.
-
-
-
Return:
-
Abstract path An array group, which represents the files and directories in the directory represented by this abstract path name. If the directory is empty, then the array will also be empty. Returns if the abstract path name does not represent a directory, or an I/O error occurs
null
.
-
Thrown:
-
SecurityException
-If a security manager is present and its
SecurityManager.checkRead(java.lang.String)
methods deny read access to the directory
Using recursive output files and folders, first the files under the specified directory are all traversed and placed in the File[]数组中,然后递归调用输出这个数组的所有元素。
Java recursively outputs all files and folders under the specified path