/**
* filePath文件中的属性用propFilePath文件中具有相同key的value值替换
* @param filePath 为需要替换属性的文件路径
* @param propFilePath properties文件路径
* @return 是否wan全部替换成功*/
public staticboolean fileAttributesReplace(String filePath,String propFilePath){if(org.apache.commons.lang.StringUtils.isEmpty(filePath) ||org.apache.commons.lang.StringUtils.isEmpty(propFilePath) ){
LOG.error("filePath = {} or propFilePath = {} is empty",filePath,propFilePath);return false;
}if(!new File(filePath).exists() || !newFile(propFilePath).exists()){
LOG.error("filePath = {} or propFilePath = {} 不存在",filePath,propFilePath);return false;
}//把文件内容全部读取到List中,然后做属性替换,替换完成后,重新写文件
List list =readFileToList(filePath);
Map map =readPropFileToMap(propFilePath);
List result =attributesReplace(list,map);returnwriteListToFile(result,filePath);
}/**
* 属性替换
* @param list
* @param map
* @return*/
public static List attributesReplace(List list, Mapmap){if(list.size() < 1 || map.size() < 1){returnlist;
}
List result = new ArrayList<>();//生成匹配模式的正则表达式
String patternString = "\\$\\{(" + org.apache.commons.lang.StringUtils.join(map.keySet(), "|") + ")\\}";
Pattern pattern=Pattern.compile(patternString);for(String template : list) {
Matcher matcher=pattern.matcher(template);//两个方法:appendReplacement, appendTail
StringBuffer sb = newStringBuffer();while(matcher.find()) {
matcher.appendReplacement(sb, map.get(matcher.group(1)));
}
matcher.appendTail(sb);
result.add(sb.toString());
}returnresult;
}/**
* 把list内容写入到文件中
* @param list
* @param filePath
* @return*/
public static boolean writeListToFile(Listlist,String filePath){
BufferedWriter writer= null;try{if(newFile(filePath).exists()){if(!newFile(filePath).delete()){
LOG.error("删除文件:{}失败", filePath);
}
}
writer= new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath, true), UTF8_CHARSET));for(String temp : list) {
writer.write(temp+ "\n");
}
writer.flush();return true;
}catch(IOException e) {
LOG.error("写文件:{} 出现IO异常,异常信息:{}",filePath,e.getMessage());return false;
}finally{if(null !=writer){try{
writer.close();
}catch(IOException e) {
LOG.error("文件:{},关闭文件流时发生异常:{}", filePath, e.getMessage());
}
}
}
}/**
* 读取文件内容放到List
* @param filePath
* @return*/
public static ListreadFileToList(String filePath){if(org.apache.commons.lang.StringUtils.isEmpty(filePath)){
LOG.error("filePath = {} is empty",filePath);return new ArrayList<>();
}if(!newFile(filePath).exists() ){
LOG.error("filePath = {} 不存在",filePath);return new ArrayList<>();
}
BufferedReader reader= null;try{
reader= new BufferedReader(new InputStreamReader(new FileInputStream(newFile(filePath)), UTF8_CHARSET));
String tempString;
List list = new ArrayList<>();while ((tempString = reader.readLine()) != null) {
list.add(tempString);
}returnlist;
}catch(FileNotFoundException e){
LOG.error("filePath = {} 不存在",filePath);return new ArrayList<>();
}catch(IOException e){
LOG.error("读取filePath = {} 文件发生异常,异常信息:{}",filePath,e.getMessage());return new ArrayList<>();
}finally{if ( null !=reader ) {try{
reader.close();
}catch(IOException e) {
LOG.error("文件:{},关闭文件流时发生异常:{}", filePath, e.getMessage());
}
}
}
}/**
* 拷贝src文件成dst文件
* @param src
* @param dst
* @return*/
public staticboolean copy(String src,String dst){
BufferedReader reader= null;
BufferedWriter writer= null;try{
reader= new BufferedReader(new InputStreamReader(newFileInputStream(src), UTF8_CHARSET));
String temp;
writer= new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dst, true), UTF8_CHARSET));while ((temp = reader.readLine()) != null) {
writer.write(temp+ "\n");
}
writer.flush();
LOG.info("复制文件从src ={} 到dst = {} 成功", src, dst);return true;
}catch(IOException e) {
LOG.error("复制文件从src ={} 到dst = {} 出现IO异常,error:{}",src,dst,StringUtils.getMsgFromException(e));return false;
}finally{if(null !=reader){try{
reader.close();
}catch(IOException e) {
LOG.error("文件:{},关闭文件流时发生异常:{}", src, e.getMessage());
}
}if(null !=writer){try{
writer.close();
}catch(IOException e) {
LOG.error("文件:{},关闭文件流时发生异常:{}", dst, e.getMessage());
}
}
}
}/**
* 读取properties文件内容转成map
* @param propFilePath properties文件路径
* @return map*/
public static MapreadPropFileToMap(String propFilePath){
Map map = new HashMap<>();if(org.apache.commons.lang.StringUtils.isEmpty(propFilePath)){
LOG.error("propFilePath = {} is empty",propFilePath);returnmap;
}
File propFile= newFile(propFilePath);if(!propFile.exists()){
LOG.error("propFilePath = {} 不存在",propFilePath);returnmap;
}
Properties prop= newProperties();try{
prop.load(new BufferedReader(new InputStreamReader(newFileInputStream(propFile), Charset.forName(Constants.UTF8))));
}catch(FileNotFoundException e){
LOG.error("propFilePath = {} 不存在",propFilePath);returnmap;
}catch(IOException e) {
LOG.error("加载propFilePath = {} 文件出现异常,异常信息:{}",propFilePath,e.getMessage());returnmap;
}for (Map.Entryentry : prop.entrySet()) {
map.put(((String)entry.getKey()).trim(),((String)entry.getValue()).trim());
}returnmap;
}