Java之递归遍历目录,修改指定文件的指定内容

EditProperties.java

 1 package PropertiesOperation.Edit;
 2 
 3 import java.io.File;
 4 
 5 /**
 6  * 替换指定Porpoerties文件中的指定内容
 7  * 三个参数:
 8  * filePath:存放properties文件的目录
 9  * srcStr:需要替换的字符串
10  * desStr:用于替换的字符串
11  * */
12 public class EditProperties {
13     private static int num = 0; // 计数变量
14     public static void main(String[] args) {
15         String filePath = "C:\workspace\work\ShanDianDaiTools\src\main\" +
16                 "resource\接口测试\app启动次数统计接口";
17         String srcStr = "bd.test.com:8888";   //需要替换的字符串
18         String desStr = "10.15.1.200:8580";   //用于替换的字符串
19 
20         editProperties(filePath, srcStr, desStr);
21         System.out.println("总共文件数:" + num);
22     }
23 
24     public static void editProperties(String filePath, String srcStr, String desStr) {
25         File file = new File(filePath);
26 //        处理目录情况
27         if (file.isDirectory()) {
28             File[] subFiles = file.listFiles();
29             for (File subFile : subFiles) {
30 //                子文件如果是目录进行递归
31                 if (subFile.isDirectory()) {
32                     editProperties(subFile.getAbsolutePath(), srcStr, desStr);
33                 } else {
34 //                    子文件如果是文件,通过后缀名进行过滤
35                     if (subFile.getName().endsWith(".properties")) {
36                         System.out.println(subFile.getAbsolutePath());
37                         EditFile.propertiesChange(subFile.getAbsolutePath(), srcStr, desStr);
38                         num++;
39                     } else {
40                         continue;
41                     }
42                 }
43             }
44         } else {
45             // 处理单个文件情况
46             if (file.getName().endsWith(".properties")) {
47                 System.out.println(file.getAbsolutePath());
48                 EditFile.propertiesChange(file.getAbsolutePath(), srcStr, desStr);
49                 num++;
50             }
51         }
52     }
53 }

EditFile.java

  1 package PropertiesOperation.Edit;
  2 
  3 import java.io.*;
  4 import java.util.ArrayList;
  5 import java.util.List;
  6 
  7 /**
  8  * 修改文件中的内容* 两种情况:1.修改文件中的指定内容;2.读取文件并修改指定内容,复制到另一个文件中
  9  * 场景举例:替换properties文件中的ip和端口
 10  */
 11 public class EditFile {
 12     /**
 13      * 1.修改文件中的指定内容
 14      * filePath:文件路径
 15      * srcStr:需要替换的字符串
 16      * desStr:替换成的字符串
 17      */
 18     public static void propertiesChange(String filePath, String srcStr, String desStr) {
 19         //字符流
 20         FileReader fr = null;
 21         FileWriter fw = null;
 22         //缓冲流
 23         BufferedReader br = null;
 24         BufferedWriter bw = null;
 25 
 26         List list = new ArrayList<>();
 27         //读取文件内容保证在list中
 28         try {
 29             fr = new FileReader(new File(filePath));
 30             br = new BufferedReader(fr);   //扩容,类似加水管
 31             String line = br.readLine();    //逐行复制
 32             while (line != null) {
 33                 //修改指定内容
 34                 if (line.contains(srcStr)) {
 35                     line = line.replace(srcStr, desStr);
 36                 }
 37                 list.add(line);
 38                 line = br.readLine();
 39             }
 40         } catch (IOException e) {
 41             e.printStackTrace();
 42         } finally {
 43             try {
 44                 //关闭流,顺序与打开相反
 45                 br.close();
 46                 fr.close();
 47             } catch (IOException e) {
 48                 e.printStackTrace();
 49             }
 50         }
 51 
 52         //将list中内容输出到原文件中
 53         try {
 54             fw = new FileWriter(filePath);
 55             bw = new BufferedWriter(fw);
 56             for (Object s : list) {
 57                 bw.write((String) s);
 58                 bw.newLine();  //换行输出
 59             }
 60             System.out.println("文件修改成功!");
 61         } catch (IOException e) {
 62             e.printStackTrace();
 63         } finally {
 64             try {
 65                 //关闭流,顺序与打开相反
 66                 bw.close();
 67                 fw.close();
 68             } catch (IOException e) {
 69                 e.printStackTrace();
 70             }
 71         }
 72     }
 73 
 74     /**
 75      * 2.读取文件并修改指定内容,复制到另一个文件中
 76      * inputPath:修改的源文件
 77      * outputPath:修改后输出的文件路径
 78      * srcStr:需要替换的字符串
 79      * desStr:替换成的字符串
 80      */
 81     public static void propertiesChange(String inputPath, String outputPath, String srcStr, String desStr) {
 82         //字符流
 83         FileReader fr = null;
 84         FileWriter fw = null;
 85         //缓冲流
 86         BufferedReader br = null;
 87         BufferedWriter bw = null;
 88 
 89         try {
 90             fr = new FileReader(new File(inputPath));
 91             br = new BufferedReader(fr);   //扩容,类似加水管
 92             fw = new FileWriter(outputPath);
 93             bw = new BufferedWriter(fw);
 94 
 95             String line = br.readLine();    //逐行复制
 96             while (line != null) {
 97                 if (line.contains(srcStr)) {
 98                     line = line.replace(srcStr, desStr);
 99                 }
100                 bw.write(line);
101                 bw.newLine();  //换行输出
102                 line = br.readLine();
103             }
104             System.out.println("文件修改成功!");
105         } catch (IOException e) {
106             e.printStackTrace();
107         } finally {
108             try {
109                 //关闭流,顺序与打开相反
110                 bw.close();
111                 br.close();
112                 fw.close();
113                 fr.close();
114             } catch (IOException e) {
115                 e.printStackTrace();
116             }
117         }
118     }
119 
120 }
原文地址:https://www.cnblogs.com/gongxr/p/8094508.html