黑马程序员——IO概述之字节流和流对象缓冲技术

------ Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------

一、字节流

进行字节传输的流被称为字节流,其中,FileInputStrean和FileOutputStream是java中最常用的两个字节流。FileInputStream是字节读取流,FileOutputStream是字节写入流。

二、FileInputStream和FileOutputStream的基本使用方式
如果要把C盘下的图片文件或者Mp3文件或者视频文件复制到D盘下,之前学过的字符流FileReader和FileWriter将不再能用,这时只能通过FileInputStrean和FileOutputStream两个字节流的组合使用来完成。代码演示如下。
/*
 * 需求:把c盘下的Mp3文件复制到D盘下
 * 思路:
 * 1.创建字节读取流对象,并关联到mp3文件
 * 2.创建字节输入流对象,并在D盘下创建一个空的MP3文件
 * 3.通过循环读取和写入,完成数据传输。
 */
package com.itheima;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;



class Demo{
	public static void main(String[] args){
		FileInputStream  fis = null;
		FileOutputStream fos = null;
		try
		{
			//创建两个字节流对象并关联文件
			fis=new FileInputStream("c:\\123.mp3");
			fos=new FileOutputStream("d:\\456.mp3");
			byte[] buf = new byte[1024];
			int len = 0;
			while((len=fis.read())!=-1){  //循环读取写入操作
				fos.write(buf,0,len);
			}
		}
		catch(IOException e)
		{
			throw new RuntimeException("文件复制失败");
		}
		finally//关闭流资源
		{
			try
			{
				if(fis!=null)
					fis.close();
			}
			catch(IOException e)
			{
				throw new RuntimeException("读取关闭失败");
			}
			try
			{
				if(fos!=null)
					fos.close();
			}
			catch(IOException e)
			{
				throw new RuntimeException("写入关闭失败");
			}
		}		
	}
}

三、流对象缓冲技术

所谓流对象缓冲技术,即BufferedWriter、BufferedReader、BufferedInputStream和BufferedOutputStream四个类。这四个类的存在是为了增强流对象的读取和写入功能,从而提高数据传输的效率。
需要注意:这四个类是基于流对象而存在的,四个类在实例化时必须接收相应的流对象进来。下面通过BufferedInputStream和BufferedOutputStream演示流对象缓冲技术
/*
 * 需求:把c盘下的Mp3文件复制到D盘下,要求使用缓冲技术
 * 思路:
 * 1.创建字节读取流缓冲类对象,传入读取流并关联到mp3文件
 * 2.创建字节输入流缓冲类对象,传入写入流并在D盘下创建一个空的MP3文件
 * 3.通过两个缓冲类对象,完成数据传输。
 */
package com.itheima;

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



class Demo{
	public static void main(String[] args){
		
		//声明两个缓冲类对象的引用
		BufferedInputStream  buis  = null;
		BufferedOutputStream buos  = null;
		try
		{
			//创建两个缓冲类对象并传入流对象
			buis =new BufferedInputStream(new FileInputStream("c:\\123.mp3"));
			buos =new BufferedOutputStream(new FileOutputStream("D:\\789.mp3"));
			int by =0;
			while((by=buis.read())!=-1){    //通过缓冲类对象完成循环读取写入
				buos.write(by);
			}
		}
		catch(IOException e){
			throw new RuntimeException("文件复制失败");
		}
		finally //通过关闭缓冲类对象关闭流资源
		{
			try{
				if(buis!=null)
					buis.close();
			}
			catch(IOException e){
				throw new RuntimeException("读取关闭失败");
			}
			try{
				if(buos!=null)
					buos.close();
			}
			catch(IOException e){
				throw new RuntimeException("写入关闭失败");
			}
		}
	}
		
}

四、缓冲类的设计原理

缓冲类,专业术语叫做装饰类,是一种为了增强已有类功能而存在的类,装饰类的存在使得已有类不用衍生子类就能达到增强功能的目的,避免了继承体系的臃肿。



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值