java递归遍历获取目录下所有文件
import java.io.File; import java.util.ArrayList; import java.util.List; public class GetFiles { ArrayList<Object> list=new ArrayList<Object>(); public List<Object> getFiles(File file){ File[] a=file.listFiles(); for(File file1:a){ if(file1.isDirectory()){ getFiles(new File(file1.getAbsolutePath())); }else if(file1.isFile()){ this.list.add(file1.getAbsolutePath()); } } return list; } public static void main(String[] args) { List<Object> list=new GetFiles().getFiles(new File("C:/software/workspace")); for(Object obj:list){ System.out.println(obj); } System.out.println("文件总数:"+list.size()); } }