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: em1.png, E: em20192208092223.png, E: em em120195308105326.png]