Java遍曆檔案夾下所有檔案並替換指定字串

來源:互聯網
上載者:User

標籤:java   遍曆檔案夾   替換   

應用情境:比如有一個深層次的檔案目錄結構,如:javaAPI

每個檔案裡面都有相同的內容,而我們要統一修改為其他內容。上千個檔案如果一個個修改顯得太不明智。

import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.InputStreamReader;import java.io.PrintWriter;public class Test {    /**     * @author itmyhome     */    public static void main(String[] args) {        File f = new File("F:/javaAPI/java/lang/reflect");        print(f, 0);    }    /**     * 遍曆檔案夾     *     * @param f     * @param len     */    public static void print(File f, int len) {        File[] file = f.listFiles();        for (int i = 0; i < file.length; i++) {            if (file[i].isDirectory()) { //判斷是否檔案夾                print(file[i], len + 1);            }            // 為防止輸出檔案覆蓋源檔案,所以更改輸出盤路徑 也可自行設定其他路徑            File outPath = new File(file[i].getParent().replace("F:", "E:"));            File readfile = new File(file[i].getAbsolutePath());            if (!readfile.isDirectory()) {                String filename = readfile.getName(); // 讀到的檔案名稱                String absolutepath = readfile.getAbsolutePath(); // 檔案的絕對路徑                readFile(absolutepath, filename, i, outPath); // 調用 readFile            }        }    }    /**     * 讀取檔案夾下的檔案     *     * @return     */    public static void readFile(String absolutepath, String filename,        int index, File outPath) {        try {            BufferedReader bufReader = new BufferedReader(new InputStreamReader(                        new FileInputStream(absolutepath), "gb2312")); // 資料流讀取檔案            StringBuffer strBuffer = new StringBuffer();            String newStr = "i love you too";            String oldStr = "i love you";            for (String temp = null; (temp = bufReader.readLine()) != null;                    temp = null) {                if ((temp.indexOf(oldStr) != -1) &&                        (temp.indexOf(oldStr) != -1)) { // 判斷當前行是否存在想要替換掉的字元                    temp = temp.replace(oldStr, newStr); // 此處進行替換                }                strBuffer.append(temp);                strBuffer.append(System.getProperty("line.separator")); // 分行符號            }            bufReader.close();            if (outPath.exists() == false) { // 檢查輸出檔案夾是否存在,若不存在先建立                outPath.mkdirs();                System.out.println("已成功建立輸出檔案夾:" + outPath);            }            PrintWriter printWriter = new PrintWriter(outPath + "\\" +                    filename, "gb2312"); // 替換後輸出檔案路徑            printWriter.write(strBuffer.toString().toCharArray()); //重新寫入            printWriter.flush();            printWriter.close();            System.out.println("第 " + (index + 1) + " 個檔案   " + absolutepath +                "  已成功輸出到    " + outPath + "\\" + filename);        } catch (Exception e) {            e.printStackTrace();        }    }}


itmyhome 


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

Java遍曆檔案夾下所有檔案並替換指定字串

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.