文件特定内容替换的小程序

前端之家收集整理的这篇文章主要介绍了文件特定内容替换的小程序前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

昨天,有个小需求,要将一些xml文件中的内容进行替换,为了省时间,写了个小程序进行处理 ^_^,记录下,以备不时之需!


package com.bbwl; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class FileTest { public static void main(String[] args){ changeContent(); } public static void changeContent() { String filePath = null; Scanner scanner = new Scanner(System.in); filePath = scanner.nextLine(); FileReader fileReader = null; BufferedReader reader = null; FileWriter fileWriter = null; BufferedWriter writer = null; try { StringBuffer bufferOriginal = new StringBuffer(); StringBuffer bufferNew = new StringBuffer(); int dotIndex = filePath.lastIndexOf("."); Calendar calendar = Calendar.getInstance(); SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyyMMdd"); String destinationFilePath = filePath.substring(0,dotIndex) + "-" + simpleFormat.format(calendar.getTime()) + filePath.substring(dotIndex); fileWriter = new FileWriter(destinationFilePath); writer = new BufferedWriter(fileWriter); fileReader = new FileReader(filePath); reader = new BufferedReader(fileReader); String original = ""; String destination = ""; while((original = reader.readLine())!=null){ destination = original; if(original.contains("<isNotNull")){ int splitIndex = original.indexOf(">"); String partOne = original.substring(0,splitIndex); String partTwo = original.substring(splitIndex); String property = partOne.substring(partOne.indexOf("property")).split("\"")[1]; destination = partOne.substring(0,partOne.indexOf("<isNotNull")) + "<if test=\"" + property + " != null\"" + partTwo; System.out.println("property=" + property); System.out.println(destination); } if(original.contains("</isNotNull>")){ destination = destination.replaceAll("</isNotNull>","</if>"); } String regex = "#([^#}]+)#"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(destination); while(matcher.find()){ String replaceWord = matcher.group(0); String replaceWordValue = matcher.group(1); System.out.println("replaceWord: " + replaceWord); System.out.println("replaceWordValue: " + replaceWordValue); int replaceStartIndex = destination.indexOf(replaceWord); int replaceEndIndex = replaceStartIndex + replaceWord.length(); destination = destination.substring(0,replaceStartIndex) + "#{" + replaceWordValue + "}" + destination.substring(replaceEndIndex); System.out.println("pp: " + destination); } writer.write(destination); writer.newLine(); writer.flush(); bufferOriginal.append(original + "\n"); bufferNew.append(destination + "\n"); } System.out.println("Destination filePath: " + destinationFilePath); System.out.println("==== Origial:"); System.out.println("==== Result: "); } catch (Exception e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (Exception e) { e.printStackTrace(); } } if (fileReader != null) { try { fileReader.close(); } catch (Exception e) { e.printStackTrace(); } } if (writer != null) { try { writer.close(); } catch (Exception e) { e.printStackTrace(); } } if (fileWriter != null) { try { fileWriter.close(); } catch (Exception e) { e.printStackTrace(); } } } } }

原文链接:https://www.f2er.com/regex/361702.html

猜你在找的正则表达式相关文章