io输入输出与反射机制2

 

package com.io_zuoye;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

//替换文本里的文字
public class ZuoYe1 {

    public static void main(String[] args) {
        FileReader fr =null;
        BufferedReader br = null;
        FileWriter fw =null;
        BufferedWriter bw = null;
        try {
            fr = new FileReader("e:/java/zuoye.txt");
            br = new BufferedReader(fr);

            StringBuffer sbf = new StringBuffer();
            String str ="";
            while((str=br.readLine())!=null) {
                sbf.append(str);
            }
            System.out.println("替换前:"+sbf);
            String newsbf=sbf.toString().replace("{name}", "欧欧").replace("{type}", "狗狗").replace("{master}", "李伟");
            System.out.println("替换后:"+newsbf);

            fw = new FileWriter("zuoye1");
            bw = new BufferedWriter(fw);
            bw.write(newsbf);
            bw.flush();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            try {
                bw.close();
                fw.close();
                br.close();
                fr.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }
}

io输入输出与反射机制2

 

上一篇:实现字符串的替换


下一篇:java将文本写入本地硬盘