您的位置:首页 > 其它

遍历一个指定路径下的所有文件

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);
}
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: