java文本替换(单文件)

Java代码 1. import java.io.BufferedReader; 2. import java.io.BufferedWriter; 3. import java.io.File; 4. import java.io.FileNotFoundException; 5. import java.io.FileReader; 6. import java.io.FileWriter; 7. import java.io.IOException; 8. 9. public class StringRpl { 10. 11. public static String read(File src) { 12. StringBuffer res = new StringBuffer(); 13. String line = null; 14. try { 15. BufferedReader reader = new BufferedReader(new FileReader(src)); 16. while ((line = reader.readLine()) != null) { 17. res.append(line + "/n"); 18. } 19. reader.close(); 20. } catch (FileNotFoundException e) { 21. e.printStackTrace(); 22. } catch (IOException e) { 23. e.printStackTrace(); 24. } 25. return res.toString(); 26. } 27. 28. public static boolean write(String cont, File dist) { 29. try { 30. BufferedWriter writer = new BufferedWriter(new FileWriter(dist)); 31. writer.write(cont); 32. writer.flush(); 33. writer.close(); 34. return true; 35. } catch (IOException e) { 36. e.printStackTrace(); 37. return false; 38. } 39. } 40. 41. public StringRpl() { 42. } 43. 44. public static void main(String[] args) { 45. File src = new File("a.txt"); 46. String cont = StringRpl.read(src); 47. System.out.println(cont); 48. // 对得到的内容进行处理 49. cont = cont.replaceAll("public", "private"); 50. System.out.println(cont); 51. // 更新源文件 52. System.out.println(StringRpl.write(cont, src)); 53. } 54. 55. }

替换结果: Java代码 1. import java.io.BufferedReader; 2. import java.io.BufferedWriter; 3. import java.io.File; 4. import java.io.FileNotFoundException; 5. import java.io.FileReader; 6. import java.io.FileWriter; 7. import java.io.IOException; 8. 9. private class StringRpl { 10. 11. private static String read(File src) { 12. StringBuffer res = new StringBuffer(); 13. String line = null; 14. try { 15. BufferedReader reader = new BufferedReader(new FileReader(src)); 16. while ((line = reader.readLine()) != null) { 17. res.append(line + "/n"); 18. } 19. reader.close(); 20. } catch (FileNotFoundException e) { 21. e.printStackTrace(); 22. } catch (IOException e) { 23. e.printStackTrace(); 24. } 25. return res.toString(); 26. } 27. 28. private static boolean write(String cont, File dist) { 29. try { 30. BufferedWriter writer = new BufferedWriter(new FileWriter(dist)); 31. writer.write(cont); 32. writer.flush(); 33. writer.close(); 34. return true; 35. } catch (IOException e) { 36. e.printStackTrace(); 37. return false; 38. } 39. } 40. 41. private StringRpl() { 42. } 43. 44. private static void main(String[] args) { 45. File src = new File("a.txt"); 46. String cont = StringRpl.read(src); 47. System.out.println(cont); 48. // 对得到的内容进行处理 49. cont = cont.replaceAll("private", "private"); 50. System.out.println(cont); 51. // 更新源文件 52. System.out.println(StringRpl.write(cont, src)); 53. } 54. 55. } 56. 57. true import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; private class StringRpl { private static String read(File src) { StringBuffer res = new StringBuffer(); String line = null; try { BufferedReader reader = new BufferedReader(new FileReader(src)); while ((line = reader.readLine()) != null) { res.append(line + "/n"); } reader.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return res.toString(); } private static boolean write(String cont, File dist) { try { BufferedWriter writer = new BufferedWriter(new FileWriter(dist)); writer.write(cont); writer.flush(); writer.close(); return true; } catch (IOException e) { e.printStackTrace(); return false; } } private StringRpl() { } private static void main(String[] args) { File src = new File("a.txt"); String cont = StringRpl.read(src); System.out.println(cont); // 对得到的内容进行处理 cont = cont.replaceAll("private", "private"); System.out.println(cont); // 更新源文件 System.out.println(StringRpl.write(cont, src)); } } true

你可能感兴趣的:(java文本替换(单文件))