替换文本文件内容

 

 

package Zuoye01;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

public class CopyDogTest01 {

    public static void main(String[] args) {
        
        //我的名字是{name},我是一只{type},我的主人是{master}
        //1 地址  2 读取  - 文本(修改)  输出  - 控制台  -  文本
        
        Reader re = null;
        BufferedReader er = null;
        
        Writer wi = null;
        BufferedWriter bw = null;

    
        
        try {
            re = new FileReader("D:/Dog.txt");
            er = new BufferedReader(re);
            try {
                wi = new FileWriter("D:/D.txt");
                bw = new BufferedWriter(wi);

                
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            
//            StringBuffer sbf = new StringBuffer();
            String line = null;
            try {
                while((line=er.readLine())!=null) {
                    
//                     sbf.append(line);
                    System.out.println("替换前的:"+line);
                    
                    line = line.replace("{Name}", "欧欧");
                    line = line.replace("{Type}", "狗狗");
                    line = line.replace("{Master}", "李伟");
                    
                    
                    System.out.println("替换后的:"+line);
                    bw.write(line);
                    bw.flush();
                    
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            
            
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            
            try {
                wi.close();
                bw.close();
                
                re.close();
                er.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        
            
            
        }
        
        
        
    }
    
    
    
}

 

 

posted @ 2019-08-03 00:18  AxeBurner  阅读(411)  评论(0编辑  收藏  举报