替换Java WEB工程文件的指定字符串

package com.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ReplaceKeyWordUtil {

private static String i18N_ZH_CN = "i18n.rmsMessage_zh_CN";
private static String i18N_ZH_TW = "i18n.rmsMessage_zh_TW";
private static String rootPath = "E:\ideaSpace\i18n_for_HK\rms-web\irms.web.itms\src\main\java\com\boco\authority\action";
private static String fileExt = ".java";

/**
* 解析文件内容
* @param file
* @return 一个文件 xxx.java
* @throws Exception
*/
public static String parserSourceFile(File file) throws Exception {
BufferedReader br = new BufferedReader(new FileReader(file));
char[] buffer = new char[(int) file.length()];
br.read(buffer, 0, (int) file.length());
return new String(buffer, 0, buffer.length);
}

/**
* 根据模型正则匹配结果
* @param rex [\u4e00-\u9fa5]
* @return List
*/
public static String pattMatchAndReplace(String text, String rex,String replaceTarg,File file) throws FileNotFoundException {
Pattern pat = Pattern.compile(rex);
Matcher mat = pat.matcher(text);
if (mat.find()) {
PrintWriter pw = new PrintWriter(file);
pw.print(mat.replaceAll(replaceTarg));
pw.close();
}
return null;
}



/**
* 根据目标目录获取文件内容
*
* @param rootPath
* @param result
* @param fileExt
* @return List 文件结果集
*/
public static List<File> getFiles(String rootPath, List<File> result, final String fileExt) {
File f = new File(rootPath);
File[] list = f.listFiles(new FileFilter() {
@Override
public boolean accept(File f) {
boolean ret = f.isDirectory() || (f.isFile() && f.getName().endsWith(fileExt));
return ret;
}
});
for (File fn : list) {
if (fn.isDirectory()) {
ReplaceKeyWordUtil.getFiles(fn.getAbsolutePath(), result, fileExt);
} else {
result.add(fn);
}
}
return result;
}

/**
* 读取指定properties文件,组装成Map返回
*
* @param proName "i18n.rmsMessage_zh_TW" ,"rmsMessage_zh_CN",
* 这里的配置文件在maven项目工程的resource下或者在其下的某个文件夹里(这里是i18n),
* 传入的参数为文件名去掉.properties后缀。
* @return Map
*/
public static Map<String, String> getReadProperties(String proName) {
Map<String, String> properties = new LinkedHashMap<>();
ResourceBundle rbProResults = ResourceBundle.getBundle(proName);
Enumeration<String> keys = rbProResults.getKeys();
while (keys.hasMoreElements()) {
String key = keys.nextElement();
properties.put(key, rbProResults.getString(key));
}
return properties;
}



//测试替换
public static void main(String[] args) throws Exception {
Map<String, String> properties = ReplaceKeyWordUtil.getReadProperties(i18N_ZH_CN);
handleReplace(properties,fileExt);
System.out.println("ok!");
}

/**
* 执行替换
* @param properties 需要替换的关键字map集合
* @param properties 文件后缀 .java
* @param fileExt
* @throws Exception
*/
public static void handleReplace(Map properties,String fileExt) throws Exception {
//搜索目标文件
List<File> result = new LinkedList<File>();
result = ReplaceKeyWordUtil.getFiles(rootPath, result,fileExt);
System.out.println("file count :"+result.size());
//以配置文件的value为关键字,搜索匹配目标文件内容,使用MessageI18nHelper.getMessage(String key)替换该内容。
Iterator iterator = properties.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry it = (Map.Entry) iterator.next();
String key = (String) it.getKey();
String value = """ +(String)it.getValue()+""";
String replace = "MessageI18nHelper.getMessage("" + key + "")";
//System.out.println(it.getKey() + "," +it.getValue());
//查询替换关键字
for (File file : result) {
System.out.println("fileName :"+file.getName());
String sourceFile = ReplaceKeyWordUtil.parserSourceFile(file);
ReplaceKeyWordUtil.pattMatchAndReplace(sourceFile, value, replace,file);
}
}
}

/* *//**
* 根据properties结果map获取value集合
*
* @param propertis
* @return
*//*
public static Set<String> getPropertiesValues(Map<String, String> propertis) {
Set<String> valuesSet = new LinkedHashSet<>();
if (null != propertis) {
Iterator<String> iterator = propertis.keySet().iterator();
while (iterator.hasNext())
valuesSet.add(propertis.get(iterator.next()));
}
return valuesSet;
}

*//**
* 根据properties结果map获取key集合
*
* @param propertis
* @return
*//*
public static Set<String> getPropertiesKeys(Map<String, String> propertis) {
Set<String> keysSet = new LinkedHashSet<>();
if (null != propertis) {
Iterator<String> iterator = propertis.keySet().iterator();
while (iterator.hasNext())
keysSet.add(iterator.next());
}
return keysSet;
}*/
}


原文地址:https://www.cnblogs.com/pangdajin/p/8854253.html