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

本文介绍了一个用于批量替换XML文件中特定内容的小程序。该程序能够实现从<isNotNull>到<if>标签的转换,并将文本中的自定义占位符更新为标准的MyBatis表达式语法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

昨天,有个小需求,要将一些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();
}
}
}
}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值