遍历目录文件

public static List<String> listFile(List<String> lstFileNames, File f,
            String suffix, boolean isdepth) {
        // 若是目录, 采用递归的方法遍历子目录
        try {
            if (f.isDirectory()) {
                File[] t = f.listFiles();
                for (int i = 0; i < t.length; i++) {
                    if (isdepth || t[i].isFile()) {
                        listFile(lstFileNames, t[i], suffix, isdepth);
                    }
                }
            } else {
                String filePath = f.getPath();
                if (!suffix.equals("")) {
                    int begIndex = filePath.lastIndexOf("."); 
                    String tempsuffix = "";

                    if (begIndex != -1) {
                        tempsuffix = filePath.substring(begIndex + 1,
                                filePath.length());
                        if (suffix.contains("." + tempsuffix + ".")) {
                            System.out.println("filePath:"+filePath);
                            lstFileNames.add(filePath);
                        }
                    }
                } else {
                    lstFileNames.add(filePath);
                }
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return lstFileNames;
    }

 

posted @ 2017-09-07 10:54  one_person  阅读(115)  评论(0编辑  收藏  举报