替换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后,会进入如下界面,修改工程名后 ...
随机推荐
- INTERVIEW #1
一.数据对齐存储 在32位系统中:int占4Bytes,short占2Bytes,char占1Byte,加起来应该是7Bytes,但是下面这段代码输出却是8. #define _CRT_SECURE_ ...
- postman(断言)
一.断言 1.Code is 200 断言状态码是200 2.contains string 断言respoonse body中包含string 3.json value check (检查JSON值 ...
- Ajax各参数介绍及使用
Ajax各参数介绍及使用 1. url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2. type: 要求为String类型的参数,请求方式(post或get)默认为get. ...
- C语言程序设计实验报告(第一次实验)
C程序设计实验报告 实验项目:C语言程序设计教程实验1.3.2:1.3.3:1.3.4:2.3.1:2.3.2 姓名:赖瑾 实验地点:家 实验时间:2020.2.25 目录 C程序设计实验报告 一.实 ...
- Lasso回归
Lasso 是一个线性模型,它给出的模型具有稀疏的系数(sparse coefficients).它在一些场景中是很有用的,因为它倾向于使用较少参数的情况,能够有效减少给定解决方案所依赖变量的个数.因 ...
- Uncaught DOMException: Failed to set the 'value' property on 'HTMLInputElement': This input element accepts a filename, which may only be programmatically set to the empty string.
今天上传图片遇到这个报错 百度了下,网上说是input标签type=file所以导致的问题,可是我的type=hidden 解决办法: 把上面的代码改成如下问题就解决了
- 第一个Vue-cli
第一步下载node.js https://nodejs.org/zh-cn/ 安装成功后 在cmd 输入 node -v 看看能不能打印出来 在cmd 输入 nmp-v 看看能不能打印出来 全局安装 ...
- Synchronized 和 ReentrantLock (Lock )的区别
原始构成 Synchronized 是关键字,属于JVM层面,底层是通过 monitorenter 和 monitorexit 完成,依赖于 monitor 对象来完成.由于 wait/notify ...
- Matlab2016b线性规划函数linprog的几个问题
一.如何设置算法为单纯型法: options = optimoptions('linprog','Algorithm','dual-simplex') 二.linprog的参数用法: [x,Fval, ...
- haskell ide - vscode
以windows为例(因为手头只有这个系统,linux系统下类似) 1. 下载安装vscode 2. 安装haskell的管理工具stack,将路径添加到环境变量path 3. windows下安装s ...