0308作业
第一题:
package com.bd22; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.io.Writer; public class Pet { public static void main(String[] args) { InputStreamReader isr = null; BufferedReader br = null; Writer w = null; BufferedWriter bw = null; try { isr = new InputStreamReader(new FileInputStream("E:\\pet.txt"),"GBK"); w = new FileWriter("E:\\newpet.txt"); br = new BufferedReader(isr); StringBuffer sb = new StringBuffer(); bw = new BufferedWriter(w); String str = null; while((str = br.readLine())!=null) { sb.append(str); } System.out.println("替换前:"+sb); String a = sb.toString().replace("{name}", "是小白").toString().replace("{type}", "狗").toString().replace("{master}", "欣仔"); bw.write(a); bw.flush(); System.out.println("替换后:"+a); } catch (UnsupportedEncodingException | FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { try { br.close(); bw.close(); } catch (IOException e) { e.printStackTrace(); } } } }
输出结果: