Recursively list files in Java

后端 未结 27 1602
走了就别回头了
走了就别回头了 2020-11-22 00:29

How do I recursively list all files under a directory in Java? Does the framework provide any utility?

I saw a lot of hacky implementations. But none from the fra

27条回答
  •  [愿得一人]
    2020-11-22 00:41

    You can use below code to get a list of files of specific folder or directory recursively.

    public static void main(String args[]) {
    
            recusiveList("D:");
    
        }
    
        public static void recursiveList(String path) {
    
            File f = new File(path);
            File[] fl = f.listFiles();
            for (int i = 0; i < fl.length; i++) {
                if (fl[i].isDirectory() && !fl[i].isHidden()) {
                    System.out.println(fl[i].getAbsolutePath());
                    recusiveList(fl[i].getAbsolutePath());
                } else {
                    System.out.println(fl[i].getName());
                }
            }
        }
    

提交回复
热议问题