• 替换文本文件内容


    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();
                }
                
            }
            }
        
    }
  • 相关阅读:
    PM对功能模块的说明
    第四个迭代目标
    第三个迭代任务
    第三个迭代目标
    记账本状态图
    记账本数据流图
    记账本ER图
    记账本用例图
    第二个迭代目标
    记账本时序图
  • 原文地址:https://www.cnblogs.com/hxtzzz/p/11299592.html
Copyright © 2020-2023  润新知