private List<String> ergodic(File file,List<String> resultFileName){ File[] files = file.listFiles(); if(files==null)return resultFileName;// 判断目录下是不是空的 for (File f : files) { if(f.isDirectory()){// 判断是否文件夹 resultFileName.add(f.getPath()); ergodic(f,resultFileName);// 调用自身,查找子目录 }else resultFileName.add(f.getPath()); } return resultFileName; }
When calling, use: return ergodic(new File(forderPath), resultList);
The returned result is all the file paths in the directory including subdirectories, including subdirectories of subdirectories...
For more java small examples of using recursion to obtain all file paths in a directory, please pay attention to the PHP Chinese website for related articles!