Java recursively outputs all files and folders under the specified path

Source: Internet
Author: User

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

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.