两对缓冲流 BufferedInputStream BufferedOutputStream 和 BufferedReader BufferedWriter

1、BufferedInputStream和BufferedOutputStream  缓冲的字节输入输出流

2、BufferedReader 和 BufferedWriter 缓冲的字符输入输出流

3、缓冲流的特性:

1)用来提高输入输出流的效率

2)属于处理流 必须套接在字节流上使用

4、BufferedInputStream  一般套接在FileInputStream 上

BufferedOutputStream 一般套接在FileOutputStream上使用

BufferedReader  一般套接在FileReader上使用

BufferedWriter  一般套接在FileWriter上使用


用缓冲流实现文件的复制 操作代码如下:

package com.zxm.ioStream_secondday;

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

public class BufferCopeTest {

	public static void main(String[] args) {
		String path1 = "e:/wampserver_X86_2.5.1455519048.exe";
		String path2 = "e:/new_wampserver_X86_2.5.1455519048.exe";
		FileInputStream fis = null;//节点流 直接对数据进行操作
		FileOutputStream fos = null;
		//设置一个缓冲区 提高效率,将需要运输的东西放在一个缓冲区中同意运输 (即在内存中开辟一个存储空间,将读取的数据构成一个整体再输入到目标中)
		byte[] bs = new byte[2048];
		BufferedInputStream bis = null;//处理流  套接咋节点流上
		BufferedOutputStream bos = null;
		
		try {
			//创建对象
			fis = new FileInputStream(path1);//读取文件
			fos = new FileOutputStream(path2);//输出文件
			
			bis = new BufferedInputStream(fis);
			bos = new BufferedOutputStream(fos);
			int len = bis.read(bs);//一次读取了多长数据
			while(len != -1){
				bos.write(bs,0,len);//将长度为len的数据输出 使用byte[]时这样写
				len = bis.read(bs);
			}
			//输出数据 要刷新
			bos.flush();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			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();
				}
			}
			if( fis != null ){
				try {
					fis.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if( fos != null ){
				try {
					fos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

}



  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值