字节缓冲流 ( BufferedInputStream / BufferedOutputStream)

package com.sxt.reader;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/*
 * 字节缓冲流
 * BufferedInputStream
 * BufferedOutputStream
 * 实现Copy文件操作
 */
public class TestBCopy {
    public static void main(String[] args){
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            //输入流
            bis = new BufferedInputStream(new FileInputStream("F:\\马士兵.zip"));
            //输出流
            bos = new BufferedOutputStream(new FileOutputStream("G:\\CopyDest.zip"));
            //新建字节数组
            byte[] b = new byte[1024];
            //第一步:读取文件到程序
            int len = 0;
            while((len = bis.read(b)) != -1){//读取文件到数组 同时返回数据长度
                System.out.println(len);
                bos.write(b, 0, len);//第二步:再从程序读到文件   System.arraycopy
            }
            System.out.println("Copy操作完成!");
            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(bis != null){//确定流打开再关闭
                try {
                    bis.close();//关闭流
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bos != null){
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

 

posted @ 2017-04-25 19:51  清风追梦enjoy  阅读(497)  评论(0编辑  收藏  举报