工作空间下指定格式文件扫描至已TXT文件

 

public class FileSearcher {

    public static void main(String[] paramert) {
        // 在此目录中找文件D:/Programe_ee/duizhang3.5.1/BalAccCompareService
        String baseDIR = "D:/Programe_ee/duizhang3.5.1/BalAccCompareService";
        // 找扩展名为txt的文件
        String fileName = "*.java";
        String targetFileName="c:\\duizhang.txt";
       new FileSearcher().parseJavaToTxt(baseDIR, fileName, targetFileName);

    }

    /**
     *
     * 根据获取得到的java文件夹路径解析java文件到txt.
     *
     * @param baseDIR
     *            目录中找文件
     * @param fileName
     *            找扩展名为.java的文件
     */
    public void parseJavaToTxt(String baseDIR, String fileName, String targetFileName) {
        ArrayList<File> resultList = getJavaFile(baseDIR, fileName);
        BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new  FileWriter(targetFileName));
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        Integer flag=0;
        for (File file : resultList) {
            String path=file.toString().replace("\\", "/");
            try { 
                /* 创建读取对象 */ 
                FileReader fileReader = new FileReader(path); 
                /* 创建缓存区 */ 
                BufferedReader reader = new BufferedReader(fileReader);
                /* 读取文件 */ 
                String line = null; 
                while ((line=reader.readLine())!=null) {
                    if (line.trim().isEmpty()) {
                        continue;
                    }
                    if (line.trim().startsWith("//")) {
                        continue;
                    }
                    if (line.trim().contains("/*")) {
                        flag=1;
                        continue;
                    }
                    if (flag==1) {
                        if (!line.trim().contains("*/")) {
                            continue;
                        } else {
                            flag=0;
                            continue;
                        }
                    }
                    writer.append(line);
                    writer.newLine();//换行
                    writer.flush();//需要及时清掉流的缓冲区,万一文件过大就有可能无法写入了
                }
                /* 关闭对象 */ 
                reader.close();
                System.out.println("文件写入完成...");
               } catch (FileNotFoundException e) { 
                e.printStackTrace(); 
               } catch (IOException e) { 
                e.printStackTrace(); 
               } 
        }
        try {
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
       
    }

    /**
     *
     * TODO 添加方法注释.
     *
     * @param baseDIR
     *            目录中找文件
     * @param fileName
     *            找扩展名为.java的文件
     * @return ArrayList 目录下的所有java文件
     */
    public static ArrayList<File> getJavaFile(String baseDIR, String fileName) {
        ArrayList<File> resultList = new ArrayList<File>();
        FileSearcher.findFiles(baseDIR, fileName, resultList);
        if (resultList.size() == 0) {
            System.out.println("No File Fount.");
            return null;
        } else {
            for (int i = 0; i < resultList.size(); i++) {
                System.out.println(resultList.get(i));// 显示查找结果。

            }
            return resultList;
        }
    }

    /**
     * 递归查找文件
     *
     * @param baseDirName
     *            查找的文件夹路径
     * @param targetFileName
     *            需要查找的文件名
     * @param fileList
     *            查找到的文件集合
     */
    public static void findFiles(String baseDirName, String targetFileName, ArrayList<File> fileList) {
        String tempName = null;

 

        // 判断目录是否存在
        File baseDir = new File(baseDirName);
        if (!baseDir.exists() || !baseDir.isDirectory()) {
            System.out.println("文件查找失败:" + baseDirName + "不是一个目录!");
        } else {
            String[] filelist = baseDir.list();
            for (int i = 0; i < filelist.length; i++) {
                File readfile = new File(baseDirName + "\\" + filelist[i]);
                // System.out.println(readfile.getName());
                if (!readfile.isDirectory()) {
                    tempName = readfile.getName();
                    if (FileSearcher.wildcardMatch(targetFileName, tempName)) {
                        // 匹配成功,将文件名添加到结果集
                        fileList.add(readfile.getAbsoluteFile());
                    }
                } else if (readfile.isDirectory()) {
                    findFiles(baseDirName + "\\" + filelist[i], targetFileName, fileList);
                }
            }
        }
    }

    /**
     * 通配符匹配
     *
     * @param pattern
     *            通配符模式
     * @param str
     *            待匹配的字符串
     * @return 匹配成功则返回true,否则返回false
     */
    private static boolean wildcardMatch(String pattern, String str) {
        int patternLength = pattern.length();
        int strLength = str.length();
        int strIndex = 0;
        char ch;
        for (int patternIndex = 0; patternIndex < patternLength; patternIndex++) {
            ch = pattern.charAt(patternIndex);
            if (ch == '*') {
                // 通配符星号*表示可以匹配任意多个字符
                while (strIndex < strLength) {
                    if (wildcardMatch(pattern.substring(patternIndex + 1), str.substring(strIndex))) {
                        return true;
                    }
                    strIndex++;
                }
            } else if (ch == '?') {
                // 通配符问号?表示匹配任意一个字符
                strIndex++;
                if (strIndex > strLength) {
                    // 表示str中已经没有字符匹配?了。
                    return false;
                }
            } else {
                if ((strIndex >= strLength) || (ch != str.charAt(strIndex))) {
                    return false;
                }
                strIndex++;
            }
        }
        return (strIndex == strLength);
    }

}

你可能感兴趣的:(工作空间下指定格式文件扫描至已TXT文件)