1 /* 2 使用递归找出某目录("C:\\JavaProducts")下的所有子目录以及子文件 3 */ 4 5 import java.util.*; 6 import java.io.*; 7 8 public class FileRecursionPrintDemo{ 9 public static void main(String[] args){ 10 List<String> paths = new ArrayList<String>(); 11 paths = getAllFilePaths(new File("C:\\JavaProducts"),paths); 12 for(String path:paths){ 13 System.out.println(path); 14 } 15 } 16 17 private static List<String> getAllFilePaths(File filePath,List<String> filePaths){ 18 File[] files = filePath.listFiles(); 19 if(files == null){ 20 return filePaths; 21 } 22 for(File f:files){ 23 if(f.isDirectory()){ 24 filePaths.add(f.getPath()); 25 getAllFilePaths(f,filePaths); 26 }else{ 27 filePaths.add(f.getPath()); 28 } 29 } 30 return filePaths; 31 } 32 }
时间: 2024-10-06 22:28:30