標籤:緩衝流 out too oid 字元 main package exception tput
掃描目錄下檔案,修改檔案中指定內容
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 }
Java之掃描目錄,修改檔案內容