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;
}
}
}