码迷,mamicode.com
首页 > 其他好文 > 详细

字节缓冲流 ( BufferedInputStream / BufferedOutputStream)

时间:2017-04-25 21:11:25      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:java   cat   输出流   字节   fileinput   finally   缓冲流   otf   puts   

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();
                }
            }
        }
    }
}

 

字节缓冲流 ( BufferedInputStream / BufferedOutputStream)

标签:java   cat   输出流   字节   fileinput   finally   缓冲流   otf   puts   

原文地址:http://www.cnblogs.com/qingfengzhuimeng/p/6763984.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!