BufferedReader与 BufferedWriter完成copy操作

 

package com.io.writer;

import java.io.*;

/**
 * BufferedReader与 BufferedWriter完成copy操作
 */
public class BufferedCopy_ {
    public static void main(String[] args) {
        String srcFilePath="d:\\ok.txt";
        String destFilePath="d:\\ok2.txt";
        BufferedReader bufferedReader=null;
        BufferedWriter bufferedWriter=null;
        String line;
        try {
            bufferedReader= new BufferedReader(new FileReader(srcFilePath));
            bufferedWriter=new BufferedWriter(new FileWriter(destFilePath));
            //readLine是读取一行的内容,但是没有换行
            while ((line=bufferedReader.readLine())!=null){
                //每读取一行就写入
                bufferedWriter.write(line);
                //插入一个换行符
                bufferedWriter.newLine();
            }
            System.out.println("拷贝完毕");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭流
            try {
                if (bufferedReader!=null){
                    bufferedReader.close();
                }
                if (bufferedWriter!=null){
                    bufferedWriter.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }
}

 

 

 

 

 

 

 

 

 


 


 

posted @ 2022-04-30 16:48  胖虎9  阅读(65)  评论(0编辑  收藏  举报