檔案複製與替換字串

來源:互聯網
上載者:User

package com.zhoushuai.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Test {
 
 
 public static String source = "";
 public static String target = "";
 public static int FileCount = 0;
 public static int dirCount = 0;
 public static int replaceCount = 0;
 
 public static void main(String[] args){
  Date beforDate = new Date();
  File sourceFile = new File("G://dira");
  String targetDir = "G://dirb";
  List<Map<String,String>> words = new ArrayList<Map<String,String>>();
  Map replaceMap1 = new HashMap();
  replaceMap1.put("sourceString", "enableBrowserBackKey();");
  replaceMap1.put("targetString", "//enableBrowserBackKey();");
  Map replaceMap2 = new HashMap();
  replaceMap2.put("sourceString", "65367;");
  replaceMap2.put("targetString", "999999;");
  words.add(replaceMap1);
  words.add(replaceMap2);
  copyAndReplaceWord(sourceFile,targetDir,words);
  Date afterDate = new Date();
  System.out.println("複製了"+FileCount+"個檔案");
  System.out.println("複製了"+dirCount+"個檔案夾");
  System.out.println("替換了"+replaceCount+"個地方");
  long time = afterDate.getTime()-beforDate.getTime();
  System.out.println("總共使用時間為:"+time/1000);
 }
 
 
 public static void copyAndReplaceWord(File sourceFile,String targetDir,List<Map<String,String>> words){
  String sourceName = sourceFile.getAbsolutePath();
  String sourceFileName = sourceName.substring(sourceName.lastIndexOf(File.separator));
  String targetFileName = targetDir+sourceFileName;
  File targetFile = new File(targetFileName);
   if(!sourceFile.isDirectory()){
    String fileHtml = readHtmlAndReplaceWord(sourceFile,words);
     System.out.println("當前複製的檔案為:+"+sourceName);
     System.out.println("複製後的目錄為:+"+targetFileName);
     writeHtmltoTargetFile(fileHtml,targetFile);
   }else{
    if(!targetFile.exists()){
        targetFile.mkdir();
     dirCount++;
    }
    File[] files = sourceFile.listFiles();
    for(int i = 0;i<files.length;i++){
     copyAndReplaceWord(files[i],targetFileName,words);
    }
   }
 }
 
 
 public static String readHtmlAndReplaceWord(File source,List<Map<String,String>> words){
  BufferedReader bf = null;
  StringBuffer sb = new StringBuffer();
  InputStreamReader inputStreamReader = null;
  FileInputStream fileInputStream = null;
  try {
   fileInputStream = new FileInputStream(source);
   inputStreamReader = new InputStreamReader(fileInputStream);
   bf = new BufferedReader(inputStreamReader);
   String temp = null;
   String newString = null;
    while((temp = bf.readLine())!=null){
     for(int i = 0;i<words.size();i++){
      String sourceStrng = words.get(i).get("sourceString");
      if(temp.contains(sourceStrng)){
       System.out.println("該行資料為:"+temp);
       temp = temp.replace(sourceStrng, words.get(i).get("targetString"));
       System.out.println("替換後資料為:"+temp);
       replaceCount++;
      }
     }
     sb.append(temp);
     sb.append("/n");
    }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }finally{
   try {
    if(bf!=null){
     bf.close();
    }
    if(fileInputStream!=null){
     fileInputStream.close();
    }
    if(inputStreamReader!=null){
     inputStreamReader.close();
    }
   } catch (IOException e) {
    e.printStackTrace();
   }
   source = null;
  }
  return sb.toString();
 }
 
 public static void writeHtmltoTargetFile(String html,File targetFile){
  FileWriter writer;
  try {
   writer = new FileWriter(targetFile);
   writer.write(html);
   writer.close();
  } catch (IOException e) {
   e.printStackTrace();
  }finally{
   FileCount++;
   targetFile = null;
  }
 }
 
 
 

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.