原 自学JVAVA---(51)--(内功心法【49】)装饰流

博客介绍了装饰流,指出装饰流为节点流而生,节点流也有装饰的特性,二者方法调用相似是因接口简洁。还提到缓冲字符字节流,其操作效率大幅提升,字符流还能进行行内查看。

装饰流
【小城贝尔】
装饰皆为节点生,节点均是装饰心。
方法调用多相似,只因接口无多字。
缓冲字符字节流,好似蛟龙水中游。
操作效率升大半,字符新添行里看。

public class BufferdeDemo {
    public static void main(String[] args) {
        copyUseByte("E:/java1.mp4","E:/java1ChengCheng.mp4");
        copyUseRedWri("src/InnerClass/InnerClass.java","src/IODemo/copy-innerclass.java");
    }
    //Bufferded字节流文件拷贝
    public static void copyUseByte(String src , String  desc){
        InputStream in = null;
        OutputStream out = null;
        try {
           in = new BufferedInputStream(new FileInputStream(src));
           out = new BufferedOutputStream(new FileOutputStream(desc));
           byte[] bytes = new byte[1024];
           int len = -1;
           while ( (len = in.read(bytes)) != -1){
               out.write(bytes,0,len);
           }
           out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //手动关闭流 但是jdk1.7以后使用try with resource 写法自动关闭流
            closeIO(in,out);
        }
    }
    //Buffered字符流文件拷贝
    public static void copyUseRedWri(String src , String desc){
        BufferedReader in = null;
        BufferedWriter out = null;
        try {
            in = new BufferedReader(new FileReader(src));
            out = new BufferedWriter(new FileWriter(desc));
            String line = null;
            while ((line = in.readLine()) != null){
                out.write(line);
                out.newLine();
            }
            out.flush();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //手动关闭流 但是jdk1.7以后使用try with resource 写法自动关闭流
            closeIO(in,out);
        }
    }
    //因为所有的流都实现了closeable接口所以定义一个统一的方法关闭流
    public  static  void  closeIO(Closeable ...ios){
        for (Closeable io:ios){
            try {
                if (io != null){
                    io.close();
                }
                System.out.println("io has closed ");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值