遍历一个指定路径下的所有文件
2015-08-12 11:20
363 查看
import java.io.File; import java.util.ArrayList; import java.util.List; public class FileReader { static List<String> result = new ArrayList<String>(); static String path1 = "D:\\commit\\HAManager\\0807"; static String path2 = "D:\\commit\\HAManager\\0811"; public static void main(String[] args) { File file = new File(path1); File[] tempList = file.listFiles(); for (int i = 0; i < tempList.length; i++) { comeOn(tempList[i], path1); } File file2 = new File(path2); File[] tempList2 = file2.listFiles(); for (int i = 0; i < tempList2.length; i++) { comeOn(tempList2[i], path2); } for (String fileName : result) { System.out.println(fileName); } } private static void comeOn(File file, String path) { if (file.isFile()) { if (file.getName().endsWith(".js")) { String jsFile = file.toString().replace(path, "").replace("\\", "/"); if (!result.contains(jsFile)) { result.add(jsFile); } } else if (file.getName().endsWith(".class")) { String javaFile = file.toString().replace(path, "").replace("classes", "").replace("class", "java").replace("\\", "/").replace("//", "/"); if (!result.contains(javaFile)) { result.add(javaFile); } } else if (file.getName().endsWith(".jsp")) { String jspFile = file.toString().replace(path, "").replace("\\", "/"); if (!result.contains(jspFile)) { result.add(jspFile); } } else if (file.getName().endsWith(".xml")) { String xmlFile = file.toString().replace(path, "").replace("\\", "/").replace("//", "/"); if (!result.contains(xmlFile)) { result.add(xmlFile); } } else if (file.getName().endsWith(".css")){ String cssFile = file.toString().replace(path, "").replace("\\", "/").replace("//", "/"); if (!result.contains(cssFile)) { result.add(cssFile); } } else { if (!result.contains(file.toString())) { result.add(file.toString()); } } } if (file.isDirectory()) { File[] tempList = file.listFiles(); for (int i = 0; i < tempList.length; i++) { comeOn(tempList[i], path); } } } }
相关文章推荐
- UVa 12167 & HDU 2767 强连通分量 Proving Equivalences
- CSS优先级详解
- 网络图片下载到手机返回null的问题解决方法
- 关于Object类中equals方法的重写
- 关于基本的R的描述
- IE9 IE10 无法上传文件 解决方案
- 关于int *a; int &a; int & *a; int * &a
- Android Studio 中更改JDK版本
- hdoj1342 Lotto
- 键盘事件keydown、keypress、keyup分析解答
- C++11中的右值引用
- cocos图显示不出来,怀疑是太长了.然后百度了下各个硬件平台的限制
- HTML标签嵌套规则
- 社説
- hdu5375
- redhat安装ibm,rdac多路径的奇葩经历
- iOS 应用内付费(IAP)开发步骤一:填写相关的税务,银行,联系人信息;
- JAVA上百实例源码以及开源项目
- 大盘攻击4500需分三步走 逢回调即加仓
- DWZ富客户端框架 api