批量替换Key和value_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 批量替换Key和value

批量替换Key和value

 2014/4/14 18:46:53  your233  程序员俱乐部  我要评论(0)
  • 摘要:/***Copyright@2013transfar<br>*Allrightreserved<br>*/packagecom;importjava.io.BufferedReader;importjava.io.File;importjava.io.FileNotFoundException;importjava.io.FileReader;importjava.io.IOException;importjava.io.PrintWriter;importjava
  • 标签:KEY

?/**
?* 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();
???}
??}
?}
}

发表评论
用户名: 匿名