程序代码:
package com.bdqn; import java.io.*; public class ChangeTest { public static void main(String[] args) { FileReader fr = null; BufferedWriter bw = null; try { fr = new FileReader("e:\\hello.txt"); int result = -1; StringBuffer str = new StringBuffer(); while((result = fr.read())!=-1){ str.append((char)result); } System.out.println(str); String s = str.toString().replace("{name}","小黄"); s = s.replace("{type}","哈巴狗"); s = s.replace("前","后"); s = s.replace("{master}","大帅"); System.out.println(s); bw = new BufferedWriter(new FileWriter("e:\\hello.txt")); bw.write(s); bw.flush(); bw.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { try { if(fr!=null){ fr.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
运行结果