工做空間下指定格式文件掃描至已TXT文件

 

public class FileSearcher {java

    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);緩存

    }app

    /**
     *
     * 根據獲取獲得的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();
        }
       
    }ui

    /**
     *
     * 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));// 顯示查找結果。.net

            }
            return resultList;
        }
    }code

    /**
     * 遞歸查找文件
     *
     * @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);
    }字符串

}
get

相關文章
相關標籤/搜索