标签:Map String map 程序 param static new 文本 替换
import java.io.*; import java.util.HashMap; import java.util.Map; import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Database2DatabaseUtil { //输出计数,方便观察 private static int count = 0; public static void main(String[] args) throws IOException { //只需替换Map即可 replaceAllFile("D:\\ningxinjie\\code\\b2bcode\\util\\file", "D:\\ningxinjie\\code\\b2bcode\\util\\new-File", design2OnlineMap); } //design切online static Map<String, String> design2OnlineMap = new HashMap<>(); //使用示例:design2OnlineMapPattern.put("cms01","cms01(!A)") static Map<String, String> design2OnlineMapPattern = new HashMap<>(); //online切ck static Map<String, String> doris2CkMap = new HashMap<>(); static Map<String, String> doris2CkMapPattern = new HashMap<>(); //design切ck static Map<String, String> design2DorisMap = new HashMap<>(); static Map<String, String> design2DorisMapPattern = new HashMap<>(); /** * 替换所有新文件 * * @param rootPath * @param targetPath * @param map * @return */ public static boolean replaceAllFile(String rootPath, String targetPath, Map<String, String> map) { return replaceAllFile(rootPath, targetPath, map, null); } /** * 替换所有新文件(包含正则) * * @param rootPath 原位置 * @param targetPath 目标位置 * @param map 替换选项,【如果为null则复制】 * @param mapPattern 正则选项,【如果为null则不使用正则】 * 内部调用 replaceTxt * @return */ public static boolean replaceAllFile(String rootPath, String targetPath, Map<String, String> map, Map<String, String> mapPattern) { File file = new File(rootPath); if (file.isFile()) { replaceTxt(rootPath, targetPath, true, map, mapPattern); } else if (file.isDirectory()) { //确保目录存在 File targetFile = new File(targetPath); if (!targetFile.exists() && !targetFile.isDirectory()) targetFile.mkdir(); String[] list = file.list(); for (String s : list) { replaceAllFile(rootPath + "\\" + s, targetPath + "\\" + s, map, mapPattern); } } else { System.out.println("输入异常..." + rootPath); return false; } return true; } /** * @param oldfilePath 源文本地址 * @param newfilePath 新文本地址 * @param isOverride 新文本如果存在是否覆盖 * @param map 要替换的map * @param mapPattern 正则替换(为了处理一些假如cms03 与 cms03A 我们想替换不影响,这时候就需要如cms03(?!A)这样的正则表达式来规定,如果不需要传null即可) * @return */ public static boolean replaceTxt(String oldfilePath, String newfilePath, boolean isOverride, Map<String, String> map, Map<String, String> mapPattern) { if (oldfilePath.equals("D:\\ningxinjie\\code\\b2bcode\\util\\abc\\dimension\\FindDimension.java")) { System.out.println(1); } File file = new File(oldfilePath); File newfile = new File(newfilePath); if (!isOverride && newfile.exists()) { System.out.println("源文件存在"); return false; } //判断文件存在并且是文件 Boolean isFile = file.exists() && file.isFile(); if (isFile) { BufferedReader bufferedReader = null; BufferedWriter bufferedWriter = null; try { //构造一个BufferedReader类来读取文件 bufferedReader = new BufferedReader(new FileReader(file)); //构建一个BufferedWriter类来写文件 bufferedWriter = new BufferedWriter(new FileWriter(newfilePath)); String linetxt = null; //result用来存储文件内容 StringBuilder result = new StringBuilder(); //按使用readLine方法,一次读一行 while ((linetxt = bufferedReader.readLine()) != null) { String newcontent = linetxt; if (map != null) { Set<Map.Entry<String, String>> entries = map.entrySet(); for (Map.Entry<String, String> entry : entries) { //判断是否需要使用正则 if (mapPattern != null && mapPattern.containsKey(entry.getKey())) { newcontent = regReplace(newcontent, mapPattern.get(entry.getKey()), entry.getValue()); } else { newcontent = newcontent.replace(entry.getKey(), entry.getValue());//替换 } } } bufferedWriter.write(newcontent + "\n");//\r\n //bufferedWriter.newLine(); } } catch (Exception e) { System.out.println("读取文件内容出错"); e.printStackTrace(); } finally { try { bufferedWriter.close(); bufferedReader.close(); } catch (IOException e1) { e1.printStackTrace(); } } } else { System.out.println("输入异常!源目录不是一个文件!"); return false; } System.out.println("数据输出完成" + (++count)); return true; } /** * 正则使用 * * @param content 原文本 * @param pattern 正则表达式 * @param newString 正则匹配项替换文本 * @return */ public static String regReplace(String content, String pattern, String newString) { Pattern p = Pattern.compile(pattern); Matcher m = p.matcher(content); String result = m.replaceAll(newString); return result; } /** * 创建空文件(临时创建文件,接收文本使用) * * @param path 路径 * @param fileNames 名字集合 * @throws IOException */ public static void createNewFile(String path, String[] fileNames) throws IOException { for (String fileName : fileNames) { File file = new File(path + "\\f-" + fileName); file.createNewFile(); } } }View Code
import java.io.*; import java.util.HashMap; import java.util.Map; import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Database2DatabaseUtil { //输出计数,方便观察 private static int count = 0; public static void main(String[] args) throws IOException { //只需替换Map即可 replaceAllFile("D:\\ningxinjie\\code\\b2bcode\\util\\file", "D:\\ningxinjie\\code\\b2bcode\\util\\new-File", design2OnlineMap); } //design切online static Map<String, String> design2OnlineMap = new HashMap<>(); //使用示例:design2OnlineMapPattern.put("cms01","cms01(!A)") static Map<String, String> design2OnlineMapPattern = new HashMap<>(); //online切ck static Map<String, String> doris2CkMap = new HashMap<>(); static Map<String, String> doris2CkMapPattern = new HashMap<>(); //design切ck static Map<String, String> design2DorisMap = new HashMap<>(); static Map<String, String> design2DorisMapPattern = new HashMap<>(); /** * 替换所有新文件 * * @param rootPath * @param targetPath * @param map * @return */ public static boolean replaceAllFile(String rootPath, String targetPath, Map<String, String> map) { return replaceAllFile(rootPath, targetPath, map, null); } /** * 替换所有新文件(包含正则) * * @param rootPath 原位置 * @param targetPath 目标位置 * @param map 替换选项,【如果为null则复制】 * @param mapPattern 正则选项,【如果为null则不使用正则】 * 内部调用 replaceTxt * @return */ public static boolean replaceAllFile(String rootPath, String targetPath, Map<String, String> map, Map<String, String> mapPattern) { File file = new File(rootPath); if (file.isFile()) { replaceTxt(rootPath, targetPath, true, map, mapPattern); } else if (file.isDirectory()) { //确保目录存在 File targetFile = new File(targetPath); if (!targetFile.exists() && !targetFile.isDirectory()) targetFile.mkdir(); String[] list = file.list(); for (String s : list) { replaceAllFile(rootPath + "\\" + s, targetPath + "\\" + s, map, mapPattern); } } else { System.out.println("输入异常..." + rootPath); return false; } return true; } /** * @param oldfilePath 源文本地址 * @param newfilePath 新文本地址 * @param isOverride 新文本如果存在是否覆盖 * @param map 要替换的map * @param mapPattern 正则替换(为了处理一些假如cms03 与 cms03A 我们想替换不影响,这时候就需要如cms03(?!A)这样的正则表达式来规定,如果不需要传null即可) * @return */ public static boolean replaceTxt(String oldfilePath, String newfilePath, boolean isOverride, Map<String, String> map, Map<String, String> mapPattern) { if (oldfilePath.equals("D:\\ningxinjie\\code\\b2bcode\\util\\abc\\dimension\\FindDimension.java")) { System.out.println(1); } File file = new File(oldfilePath); File newfile = new File(newfilePath); if (!isOverride && newfile.exists()) { System.out.println("源文件存在"); return false; } //判断文件存在并且是文件 Boolean isFile = file.exists() && file.isFile(); if (isFile) { BufferedReader bufferedReader = null; BufferedWriter bufferedWriter = null; try { //构造一个BufferedReader类来读取文件 bufferedReader = new BufferedReader(new FileReader(file)); //构建一个BufferedWriter类来写文件 bufferedWriter = new BufferedWriter(new FileWriter(newfilePath)); String linetxt = null; //result用来存储文件内容 StringBuilder result = new StringBuilder(); //按使用readLine方法,一次读一行 while ((linetxt = bufferedReader.readLine()) != null) { String newcontent = linetxt; if (map != null) { Set<Map.Entry<String, String>> entries = map.entrySet(); for (Map.Entry<String, String> entry : entries) { //判断是否需要使用正则 if (mapPattern != null && mapPattern.containsKey(entry.getKey())) { newcontent = regReplace(newcontent, mapPattern.get(entry.getKey()), entry.getValue()); } else { newcontent = newcontent.replace(entry.getKey(), entry.getValue());//替换 } } } bufferedWriter.write(newcontent + "\n");//\r\n //bufferedWriter.newLine(); } } catch (Exception e) { System.out.println("读取文件内容出错"); e.printStackTrace(); } finally { try { bufferedWriter.close(); bufferedReader.close(); } catch (IOException e1) { e1.printStackTrace(); } } } else { System.out.println("输入异常!源目录不是一个文件!"); return false; } System.out.println("数据输出完成" + (++count)); return true; } /** * 正则使用 * * @param content 原文本 * @param pattern 正则表达式 * @param newString 正则匹配项替换文本 * @return */ public static String regReplace(String content, String pattern, String newString) { Pattern p = Pattern.compile(pattern); Matcher m = p.matcher(content); String result = m.replaceAll(newString); return result; } /** * 创建空文件(临时创建文件,接收文本使用) * * @param path 路径 * @param fileNames 名字集合 * @throws IOException */ public static void createNewFile(String path, String[] fileNames) throws IOException { for (String fileName : fileNames) { File file = new File(path + "\\f-" + fileName); file.createNewFile(); } } }
标签:Map,String,map,程序,param,static,new,文本,替换 来源: https://www.cnblogs.com/ningxinjie/p/14116694.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。