import java.io.File; public class travelAllFile { public static void main(String[] args) { String path ="E:/a"; File f = new File(path); if(f.isDirectory()){ new travelAllFile().getFileName(f); } } //递归查找函数,参数为file对象 public void getFileName(File f){ //File 数组 File[] files = f.listFiles(); for (int i = 0; i < files.length; i++) { //递归出子目录 if(files[i].isDirectory()){ System.out.println("子目录是:"+files[i].getName()); getFileName(files[i]); //递归出子文件 }else{ System.out.println(files[i].getName()); } } } }
isDirectory()方法可以判断当前是否为文件夹
0 Responses
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.