批量替换Key和value

 /**
 * Copyright @ 2013 transfar<br>
 * All right reserved<br>
 */
package com;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry;


/**
 * 将R.properties里面对应的key替换代码中的value
 * Class Name: TestMain<br>
 * Description: 类功能说明<br>
 * Sample: 该类的典型使用方法和用例<br>
 * Author: fanggt<br>
 * Date: 2013-7-2<br>
 * --------------------------------------------------<br>
 * 修改人    修改日期     修改描述<br>
 * fanggt   2013-7-2      创建<br>
 * --------------------------------------------------<br>
 * @Version  Ver1.0<br>
 */
public class TestMain {

 /**
  * 描述: <br>
  * @param args<br>
  * @throws Exception
  * @throws FileNotFoundException
  * @author:fanggt<br>
  * @date:2013-7-2<br>
  * --------------------------------------------------<br>
  * 修改人    修改日期     修改描述<br>
  * fanggt   2013-7-2      创建<br>
  * --------------------------------------------------<br>
  */
 public static void main(String[] args) throws FileNotFoundException, Exception {
  String propFile = "src/change.properties";
  Properties props = TestMain.loadR(propFile);
  File ceilFile = new File(file3);
  changeValue(ceilFile,props);
 }
 private static String file = "E:/androidWork/StartScreen/src/com/wbtech";
 private static String fileName2 = "E:/R";
 private static String file3 = "C:/Documents and Settings/Administrator/桌面/change.txt";
 
 public static Properties loadR(String propFile) throws FileNotFoundException, IOException{
  if(null == propFile){
   propFile = "src/key.properties";
  }
  Properties props = new Properties();
  props.load(new FileReader(propFile));
  return props;
 }
 public static void changeValue(File file,Map props) throws Exception{
  if(!file.exists()){
   return;
  }
  if(file.isDirectory()){
   File[] files = file.listFiles();
   if(files.length>0){
    for(File f:files){
     changeValue(f,props);
    }
   }
  }else if(file.isFile()){
   String path = file.getPath();
   BufferedReader br = new BufferedReader(new FileReader(file));
   
   String tempFileName = file.getPath()+".bak";
   File tempFile = new File(tempFileName);
   if(!tempFile.exists()){
    tempFile.createNewFile();
   }
   PrintWriter pw = new PrintWriter(tempFile,"UTF-8");
   String temp = null;
   StringBuffer buffer = new StringBuffer();
   while((temp = br.readLine())!=null){
    buffer.append(temp).append("\r\n");
   }
   String str = buffer.toString();
   Iterator it = props.entrySet().iterator();
   while(it.hasNext()){
    Map.Entry entry = (Entry) it.next();
    String key = (String) entry.getKey();
    String value = (String) entry.getValue();
    //str = str.replaceAll(value, key);
    str = str.replaceAll(key, value);
   }
   pw.write(str);
   br.close();
   pw.flush();
   pw.close();
   file.delete();
   File f = new File(path);
   tempFile.renameTo(f);
   File temp_tempFile = new File(tempFileName);
   if(temp_tempFile.exists()){
    temp_tempFile.delete();
   }
  }
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值