码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-08-12 13:05:05      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

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

}

 

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

标签:

原文地址:http://www.cnblogs.com/voctrals/p/4723702.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!