13 缓冲流+字符流 实现文本文件拷贝

package bufferedIO;

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

/*
 * 字符缓冲流  +  新增方法(不能发生多态)
 */
public class BufferedChar {
    public static void main(String[] args) {
        //创建源(仅限于字符的纯文本)
        File src = new File("E:/jar/a/2.txt");
        File dest = new File("E:/jar/a/3.txt");
        
        //选择流
        BufferedReader reader = null;
        BufferedWriter wr = null;
        
        try {
            reader = new BufferedReader(
                    new FileReader(src));
            
            wr = new BufferedWriter(
                    new FileWriter(dest));
            
            //第一种方式:普通读取操作
            /*char[] flush = new char[1024];
            int length;
            
            while((length = reader.read(flush)) != -1){
                wr.write(flush,0,length);
            }*/
            
            //第二种方式:一行行的读取
            String line = null;
            while((line = reader.readLine()) != null){
                wr.write(line);
                
                //换行符号
                //wr.append("\r\n");
                wr.newLine();
            }
            
            wr.flush();
            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println("源文件不存在");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("文件读取失败");
        }finally{
            if(reader != null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

你可能感兴趣的:(13 缓冲流+字符流 实现文本文件拷贝)