码迷,mamicode.com
首页 > 编程语言 > 详细

java文件替换写法

时间:2018-11-13 23:49:10      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:edr   mon   monit   buffer   readline   rac   文件   static   sys   

@Test public void we1() throws Exception { FileReader fileReader = new FileReader("a.txt"); FileWriter fileWriter = new FileWriter("ss.txt"); File src = new File("H:/test.txt"); String cont = Monitoring.readfileconten("H:/test.txt"); System.out.println(cont); //对得到的内容进行处理 cont = cont.replaceAll("aaa", "bbb"); System.out.println(cont); //更新源文件 System.out.println(Monitoring.write(cont, src)); } public static String readfileconten(String path) { File file = new File(path); StringBuffer stringBuffer = new StringBuffer(); String lin = null; try { BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); while ((lin = bufferedReader.readLine()) != null) { stringBuffer.append(lin + "\n"); } bufferedReader.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return stringBuffer.toString(); } public 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; } }

java文件替换写法

标签:edr   mon   monit   buffer   readline   rac   文件   static   sys   

原文地址:http://blog.51cto.com/357712148/2316596

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!