Java之扫描目录,修改文件内容

扫描目录下文件,修改文件中指定内容

  1 package org.utils.tools.fileoper;
  2 
  3 import java.io.*;
  4 import java.util.ArrayList;
  5 import java.util.List;
  6 
  7 /*
  8 * 修改文件中的内容
  9 * 替换properties文件中的ip
 10 * */
 11 public class EditFile {
 12     public static void main(String args[]) {
 13 //        String inputPath = "C:\workspace\hbase_test\src\main\resource\properties\case_01.properties";
 14 //        String outputPath = "C:\workspace\hbase_test\src\main\resource\properties\case_out.properties";
 15 
 16         String srcStr = "bd.test.com";   //需要替换的字符串
 17         String desStr = "10.15.100.25";          //用于替换的字符串
 18         // 文件目录,不扫子目录
 19         String dirPath = "C:\workspace\work\bdd-bd-test\" +
 20                 "src\test\resources\properties\case\tunny\001";
 21 
 22         File f = new File(dirPath);
 23         String wholeFilePath;
 24         String[] fileNames = f.list();
 25         for (String s : fileNames) {
 26             wholeFilePath = dirPath + "\" + s;
 27             System.out.println("处理文件:" + wholeFilePath);
 28             propertiesChange(wholeFilePath, srcStr, desStr);
 29         }
 30     }
 31 
 32     /*
 33     * 修改文件中的指定内容
 34     * */
 35     public static void propertiesChange(String filePath, String srcStr, String desStr) {
 36         //字符流
 37         FileReader fr = null;
 38         FileWriter fw = null;
 39         //缓冲流
 40         BufferedReader br = null;
 41         BufferedWriter bw = null;
 42 
 43         List list = new ArrayList<>();
 44         //读取文件内容保证在list中
 45         try {
 46             fr = new FileReader(new File(filePath));
 47             br = new BufferedReader(fr);   //扩容,类似加水管
 48             String line = br.readLine();    //逐行复制
 49             while (line != null) {
 50                 //修改指定内容
 51                 if (line.contains(srcStr)) {
 52                     line = line.replace(srcStr, desStr);
 53                 }
 54                 list.add(line);
 55                 line = br.readLine();
 56             }
 57         } catch (IOException e) {
 58             e.printStackTrace();
 59         } finally {
 60             try {
 61                 //关闭流,顺序与打开相反
 62                 br.close();
 63                 fr.close();
 64             } catch (IOException e) {
 65                 e.printStackTrace();
 66             }
 67         }
 68 
 69         //将list中内容输出到原文件中
 70         try {
 71             fw = new FileWriter(filePath);
 72             bw = new BufferedWriter(fw);
 73             for (Object s : list) {
 74                 bw.write((String) s);
 75                 bw.newLine();  //换行输出
 76             }
 77             System.out.println("文件修改成功!");
 78         } catch (IOException e) {
 79             e.printStackTrace();
 80         } finally {
 81             try {
 82                 //关闭流,顺序与打开相反
 83                 bw.close();
 84                 fw.close();
 85             } catch (IOException e) {
 86                 e.printStackTrace();
 87             }
 88         }
 89     }
 90 
 91     /*
 92     * 读取文件并修改指定内容,复制到另一个文件中
 93     * */
 94     public static void propertiesChange(String inputPath, String outputPath, String srcStr, String desStr) {
 95         //字符流
 96         FileReader fr = null;
 97         FileWriter fw = null;
 98         //缓冲流
 99         BufferedReader br = null;
100         BufferedWriter bw = null;
101 
102         try {
103             fr = new FileReader(new File(inputPath));
104             br = new BufferedReader(fr);   //扩容,类似加水管
105             fw = new FileWriter(outputPath);
106             bw = new BufferedWriter(fw);
107 
108             String line = br.readLine();    //逐行复制
109             while (line != null) {
110                 if (line.contains(srcStr)) {
111                     line = line.replace(srcStr, desStr);
112                 }
113                 bw.write(line);
114                 bw.newLine();  //换行输出
115                 line = br.readLine();
116             }
117             System.out.println("文件修改成功!");
118         } catch (IOException e) {
119             e.printStackTrace();
120         } finally {
121             try {
122                 //关闭流,顺序与打开相反
123                 bw.close();
124                 br.close();
125                 fw.close();
126                 fr.close();
127             } catch (IOException e) {
128                 e.printStackTrace();
129             }
130         }
131     }
132 
133 
134 }
原文地址:https://www.cnblogs.com/gongxr/p/7994046.html