您的位置:首页 > 编程语言 > Java开发

java遍历所有目录和文件

2015-03-06 12:39 211 查看
package xian;

import java.io.File;
import java.util.ArrayList;

public class GetFile {
private static ArrayList<String> fileList = new ArrayList<String>();
public static void main(String[] args) {
String filePath="E://小仙U盘";
getFiles(filePath);
}
/**
* 通过递归过得目录和文件
*/
public static void getFiles(String filePath){
File fileAll=new File(filePath);
File[] files=fileAll.listFiles();
for(File file:files){
if(file.isDirectory()){//如果是目录
//递归
getFiles(file.getAbsolutePath());
fileList.add(file.getAbsolutePath());
System.out.println("显示"+filePath+"下所有子目录及其文件"+file.getAbsolutePath());
}else{
System.out.println("显示"+filePath+"下所有子目录"+file.getAbsolutePath());
}
}
}
}
显示E:\小仙U盘\extjs\ExtJs包和Api\ext-4.0.7-gpl\resources\themes\images\gray\btn下所有子目录E:\小仙U盘\extjs\ExtJs包和Api\ext-4.0.7-gpl\resources\themes\images\gray\btn\btn-default-toolbar-small-focus-bg.gif
显示E:\小仙U盘\extjs\ExtJs包和Api\ext-4.0.7-gpl\resources\themes\images\gray\btn下所有子目录E:\小仙U盘\extjs\ExtJs包和Api\ext-4.0.7-gpl\resources\themes\images\gray\btn\btn-default-toolbar-small-focus-corners.gif
显示E:\小仙U盘\extjs\ExtJs包和Api\ext-4.0.7-gpl\resources\themes\images\gray\btn下所有子目录E:\小仙U盘\extjs\ExtJs包和Api\ext-4.0.7-gpl\resources\themes\images\gray\btn\btn-default-toolbar-small-focus-sides.gif
显示E:\小仙U盘\extjs\ExtJs包和Api\ext-4.0.7-gpl\resources\themes\images\gray\btn下所有子目录E:\小仙U盘\extjs\ExtJs包和Api\ext-4.0.7-gpl\resources\themes\images\gray\btn\btn-default-toolbar-small-over-bg.gif
显示E:\小仙U盘\extjs\ExtJs包和Api\ext-4.0.7-gpl\resources\themes\images\gray\btn下所有子目录E:\小仙U盘\extjs\ExtJs包和Api\ext-4.0.7-gpl\resources\themes\images\gray\btn\btn-default-toolbar-small-over-corners.gif

本文出自 “贾小仙” 博客,请务必保留此出处http://hackerxian.blog.51cto.com/9240575/1617892
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: