java中遍历制定路径下的文件夹查找出文件并打印出路径
package com.dn.UI; import java.io.File; import java.util.ArrayList; public class FindFile { public static ArrayList<File> getListFiles(Object obj) { File directory = null; if (obj instanceof File) { directory = (File) obj; } else { directory = new File(obj.toString()); } ArrayList<File> files = new ArrayList<File>(); if (directory.isFile()) { files.add(directory); return files; } else if (directory.isDirectory()) { File[] fileArr = directory.listFiles(); for (int i = 0; i < fileArr.length; i++) { File fileOne = fileArr[i]; files.addAll(getListFiles(fileOne)); } } return files; } public static void main(String[] args) { // TODO Auto-generated method stub FindFile ff = new FindFile(); ArrayList<File> files = ff.getListFiles("E:\\tem"); // SystemUtil.printFilesFilePath(files); System.out.println(files); } }
输出结果:
[E:\tem\1.png, E:\tem\20192208092223.png, E:\tem\tem1\20195308105326.png]