使用java對某些文件內容進行替換並另存在新的目錄下

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;html

public class HtmlIo {java

    public static void main(String[] args) {
        try {
            for(int k=1;k<39;k++){
            //讀取指定文件夾下的全部文件
            String filepath = "F:/areaFile file = new File(filepath);/"+k;//給我你的目錄文件夾路徑
            
            if (!file.isDirectory()) {
                System.out.println("---------- 該文件不是一個目錄文件 ----------");
            } else if (file.isDirectory()) {
                System.out.println("---------- 很好,這是一個目錄文件夾 ----------");
                String[] filelist = file.list();
                for (int i = 0; i < filelist.length; i++) {
                    File readfile = new File(filepath + "\\" + filelist[i]);
                //    String path = readfile.getPath();//文件路徑
                    String absolutepath = readfile.getAbsolutePath();//文件的絕對路徑
                    String filename = readfile.getName();//讀到的文件名
                    //////// 開始挨個的讀取文件  ////////
                    BufferedReader bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(absolutepath)));//數據流讀取文件
                    StringBuffer strBuffer = new StringBuffer();
                    String empty = "";
                    String tihuan = "";
                    for (String temp = null; (temp = bufReader.readLine()) != null; temp = null) {
//                        if(temp.indexOf("/*") != -1 && temp.indexOf("*/") != -1){ //判斷當前行是否存在想要替換掉的字符 -1表示存在
//                            tihuan = temp.substring(0, 9);//這裏截取多長本身改
//                            temp = temp.replace(tihuan, empty);//替換爲你想要的東東
//                        }
                         if (temp != null) {
                             temp = temp.replace("2009年", "2017年").replace("@chinaacc.com", "@med66.com").replace("權威", "").replace("最新", "").replaceAll("初級護士報名時間/報名地點/報名條件討論區", "護士執業資格考試報名時間/報名地點/報名條件討論區")
                             .replace("初級護士考試", "護士執業資格考試").replace("/初級護士考試報名地點/初級護士資格證考試報名條件/初級護士資格考試報名資格", "/護士考試報名地點/護士資格證考試報名條件/護士資格考試報名資格");//兩個\\表示轉義
                            
                        strBuffer.append(temp);
                        strBuffer.append(System.getProperty("line.separator"));//行與行之間的分割
                         }
                    }
                    bufReader.close();
                      File tempFile = new File("F:/html/area/"+k+"/");
                      tempFile.mkdirs();                      
                    PrintWriter printWriter = new PrintWriter("F:/html/area/"+k+"/"+filename);//替換後輸出的文件位置(切記這裏的E:/ttt 在你的本地必須有這個文件夾)
                    printWriter.write(strBuffer.toString().toCharArray());
                    printWriter.flush();
                    printWriter.close();
                    System.out.println("ok 第 " + (i+1) +" 個文件操做成功!");
                    //////// 讀取兵輸出一個文件結束  ////////
                }
                System.out.println("---------- 全部文件操做完畢 ----------");
            }
        }    
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    app

}htm

相關文章
相關標籤/搜索