package po; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; public class Pet { public static void main(String[] args) throws FileNotFoundException { FileInputStream fis=null; InputStreamReader isr=null; BufferedReader br=null; FileOutputStream fos=null; OutputStreamWriter osw=null; BufferedWriter bw=null; try { //读取 fis=new FileInputStream("D:\pet.template"); isr=new InputStreamReader(fis); br=new BufferedReader(isr); //写入 fos=new FileOutputStream("D:\pet.txt"); osw =new OutputStreamWriter(fos); bw=new BufferedWriter(osw); String str=""; StringBuffer sbf=new StringBuffer(); while((str=br.readLine())!=null){ sbf.append(str); } System.out.print("替换前:"); System.out.println(sbf.toString()); String str2=sbf.toString(); str2=str2.replace("{name}", "欧欧"); str2=str2.replace("{type}", "狗狗"); str2=str2.replace("{master}", "李伟"); System.out.print("替换后:"); System.out.println(str2); bw.write(str2); bw.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { bw.close(); osw.close(); fos.close(); br.close(); isr.close(); fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }