替换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;
}*/
}
替换Java WEB工程文件的指定字符串的更多相关文章
- 如何在java web工程下建立存储property文件的文件夹,让Java程序直接读取
如何在java web工程下建立存储property文件的文件夹,让Java程序直接读取: 步骤如下:
- Eclipse创建java web工程
Eclipse创建java web工程 eclipse版本:eclipse-jee-4.5-win32-x64 tomcat版本:apache-tomcat-7.0.63-windows-x64 jd ...
- Tomcat设置默认启动项目及Java Web工程设置默认启动页面
Tomcat设置默认启动项目 Tomcat设置默认启动项目,顾名思义,就是让可以在浏览器的地址栏中输入ip:8080,就能访问到我们的项目.具体操作如下: 1.打开tomcat的安装根目录,找到Tom ...
- java web工程发布以及解决tomcat闪退
1.tomcat闪退 a.环境变量错误 startup.bat最后假如PAUSE进入调试状态,双击startup.bat,可以看到错误,根据错误提示设置相应的环境变量,JAVA_HOME等. b.ec ...
- 于Heroku平台部署maven webapp(java web)工程
眼下,需要Heroku上述部署java web工程,该项目必须使用maven管理 一:新maven webapp工程 编者pom.xml档,增加下面的配置为例, <project xmlns=& ...
- eclipse--java工程转web工程 以及 java或java web工程转maven工程
1. 打开工程文件夹,编辑工程的.project文件. 在<natures></natures>中加入 <nature>org.eclipse.wst.commo ...
- 使用Java web工程建立Maven Web Module工程
1. 前言 之前有一篇关于搭建S2SH的文章中提到建立Maven Web Module工程,有人反馈说这个方面不会.那还是唠叨一下,写篇文章说明一下吧. 建立Maven Web Module的方式有多 ...
- 使用 maven 创建 java web 工程
本文主要讲述使用 maven 命令行的形式来创建 java web 工程 开发环境 jdk 1.7 maven 3.5.0 spring 3.2 tomcat 7 eclipse Mars Rele ...
- java web 工程创建及servlet简单使用
1.java web工程创建 (1)File--->new--->project (2)选择java enterprise,按照下图操作 (3)点击next后,会进入如下界面,修改工程名后 ...
随机推荐
- Hadoop学习笔记(三) ——HDFS
参考书籍:<Hadoop实战>第二版 第9章:HDFS详解 1. HDFS基本操作 @ 出现的bug信息 @-@ WARN util.NativeCodeLoader: Unable to ...
- CodeForces - 1102A(思维题)
https://vjudge.net/problem/2135388/origin Describe You are given an integer sequence 1,2,-,n. You ha ...
- 使用kubeadm部署k8s集群[v1.18.0]
使用kubeadm部署k8s集群 环境 IP地址 主机名 节点 10.0.0.63 k8s-master1 master1 10.0.0.63 k8s-master2 master2 10.0.0.6 ...
- Codeforces Round #632 (Div. 2)
Codeforces Round #632 (Div. 2) 这一场打的好差呀,这几次艰难上的分全部掉回去了,感觉就像一夜回到了解放前. 说实话,就是被B卡到了,没看到只能从小的放到大的... Lit ...
- IDEA 2020.1 安装教程
目录 IDEA 2020.1 安装教程 准备工作 破解教程 IDEA 2020.1 安装教程 IDEA 2020.1 安装教程 Win 10 版 64位操作系统 准备工作 IDEA旗舰版下载地址 je ...
- C# 数据操作系列 - 2. ADO.NET操作
0.前言 在上一篇中初略的介绍了一下SQL的基本写法,这一篇开始我们正式步入C#操作数据库的范围.通过这一系列的内容,我想大家能对于数据库交互有了一定的认识和基础.闲话不多说,先给大家介绍一个C#操作 ...
- OpenCV 3.4.2 Windows系统下的环境搭建(附带opencv_contrib-3.4.2)
前言 当前需要回到Windows平台下进行开发,在win10系统上搭建了编译opencv3.4.2的环境,并添加opencv_contrib-3.4.2的模块,以下是本文所需要的软件以及源码. 系统: ...
- 设计模式之GOF23状态模式
状态模式state 场景:当具有许多状态并且需要频繁改变时,用这种模式 -电梯的运行:维修,正常,自动关门,自动开门,向上运行,向下运行,消防状态 -红绿灯:红灯,黄灯,绿灯 -企业或政府系统:公文的 ...
- 自动扫雷 python
自动扫雷一般分为两种,一种是读取内存数据,而另一种是通过分析图片获得数据,并通过模拟鼠标操作,这里我用的是第二种方式. // 2018.8.10更新 代码已上传至GitHub https://gith ...
- Taro UI开发小程序实现左滑喜欢右滑不喜欢效果
前言:年后入职了一家新公司,与前同事交接完之后,发现公司有一个四端的项目(iOS,Android,H5,小程序),iOS和安卓都实现了左滑右滑的效果,而h5和小程序端没实现,询问得知前同事因网上没找到 ...