查找替换





0/5 (0投票)
本文档介绍了如何开发一个自定义 Java 工具,以便一次性在多个文件中查找和替换字符串。
引言
在日常开发中,我们有时需要一些自定义的实用程序来执行一些重复性任务,也就是自动化。 在本文中,我们将看到一个使用 Java 开发的简单实用程序,用于在一次运行中查找和替换多个文件中的字符串。
程序
让我们将此实用程序的开发分为三个部分。
- 文件备份创建
在此实用程序中,首先我们将创建子文件夹下所有文件的备份,即 backup。 - 查找和替换映射创建
我们需要从包含查找和替换字符串的 .CSV 文件创建一个映射 - 使用上一步中创建的映射,我们将在文件中查找字符串,并在文件中替换该字符串。
现在让我们深入了解每个代码块的细节
创建备份
假设我们要处理的文件位于 D:\FindReplace 中。这将是我们的源文件夹路径。
String sourceFolderPath = "D:\\FindReplace";
我们的备份位置将是 D:\FindReplace\backup。并且我假设 CSV 也位于同一文件夹中,即 D:\FindReplace,现在我们的实用程序将把替换后的文件输出到 D:\FindReplace\ReplacedFiles。
String sourceFolderPath = "D:\\FindReplace";
String destFolderPath = sourceFolderPath + "\\ReplacedFiles";
String findReplaceCSV = sourceFolderPath + "\\findReplace.csv";
现在让我们开始创建备份。要创建备份,我们需要遍历每个文件,并将文件复制到 sourceFolderPath\backup 文件夹中,使用相同的文件名和 .bak 扩展名。 下面是相同操作的代码片段
System.out.println("*****Backup Intialized******");
if(file.isFile()) {
String sourceFilePath = file.getAbsolutePath();
String bakFilePath = sourceFilePath.substring(0,sourceFilePath.lastIndexOf(File.separator)) +
"\\backup\\" +
file.getName().replace(sourceFilePath.substring(sourceFilePath.lastIndexOf("."),
sourceFilePath.length()), ".bak");
File backupFile = new File(bakFilePath);
createBackup(file, backupFile);
}
}
System.out.println("****Backup Completed*****");
上面的代码调用了 'createBackup()' 方法,该方法实际上执行复制操作。 此方法接受两个 File 参数,一个是源文件,另一个是目标文件。 下面是该方法的代码片段。
public static void createBackup(File source, File target) throws IOException{
InputStream in = new FileInputStream(source);
OutputStream out = new FileOutputStream(target);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
这完成了第一个模块。 现在让我们看看从 .CSV 文件创建查找和替换映射
FindReplace 映射
我们需要从 CSV 文件创建一个包含查找和替换字符串的映射集合。 在这里,我们将把查找字符串存储在 CSV 的映射键中,相应的替换字符串将作为键的值存储。 下面是该操作的代码片段
Map<String, String> findReplaceMap = new HashMap<String, String>();
System.out.println("****Find Replace Map Creation Started****");
findReplaceMap = createReplaceMap(findReplaceCSV);
System.out.println("****Find Replace Map Created!*****");
上面的代码片段调用了 createReplaceMap() 方法,该方法将字符串参数作为参数,该字符串将是 CSV 文件的位置,此方法读取 CSV 并创建映射并返回映射集合。
public static Map<String, String> createReplaceMap(String csvfileName){
Map<String, String> _findReplaceMap = new HashMap<String, String>();
BufferedReader br = null;
try {
String line = "";
String cvsSplitBy = ",";
br = new BufferedReader(new FileReader(csvfileName));
while ((line = br.readLine()) != null) {
// use comma as separator
String[] findReplace = line.split(cvsSplitBy);
_findReplaceMap.put(findReplace[0], findReplace[1]);
}
br.close();
}
catch(Exception e){
e.printStackTrace();
}
return _findReplaceMap;
}
System.out.println("*****Find Replace in Files Intialized******");
System.out.print("Find Replace in progress");
for(File file:listOfFiles){
if(file.isFile()){
System.out.print(".");
File convFile = new File(destFolderPath + @"\" + file.getName());
try {
convFile.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
findReplace(file, convFile, findReplaceMap);
}
}
System.out.println("");
System.out.println("****Find Replace Action Completed******");
上面的代码调用 findReplace() 方法来执行实际操作,此方法接受三个参数,前两个将是文件参数,源文件和转换后的文件,第三个参数将是 findReplace 映射。 下面是该方法的代码片段。
public static Map<String, String> createReplaceMap(String csvfileName){
Map<String, String> _findReplaceMap = new HashMap<String, String>();
BufferedReader br = null;
try {
String line = "";
String cvsSplitBy = ",";
br = new BufferedReader(new FileReader(csvfileName));
while ((line = br.readLine()) != null) {
// use comma as separator
String[] findReplace = line.split(cvsSplitBy);
_findReplaceMap.put(findReplace[0], findReplace[1]);
}
br.close();
}
catch(Exception e){
e.printStackTrace();
}
return _findReplaceMap;
}
历史
这是本文的第一个修订版。