遍历目录文件

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;
    }
原文地址:https://www.cnblogs.com/yangqimo/p/7488567.html