10.2.3 字节数组流ByteArrayInputSream&&ByteArrayOutputStream&&字节数组的“拷贝过程”

之前处理的那些流的特点都是文件,它是存储在硬盘上的,我们必须借助操作系统,所以我们接完之后,就必须通知它释放资源;
现在我们把源头(文件)换成另外一种源头,电脑上的一块内存,也就是所谓的字节数组,既然是内存,那么java是可以直接访问的,所以由垃圾回收机制来释放,所以待会我们要讲的字节数组流不用关闭;
在这里插入图片描述

所有东西都可以转为字节数组,这就是这个流的好处,因为变成了二进制,方便我们网络上进行传输;
字节数组不要太大;
我们打开API来看一下:
A ByteArrayInputStream包含一个缓冲区,其中包含可以从流中读取的字节,内部计数器跟踪由read方法提供的下一个字节;
下面的代码是字节数组输入流

package com.sxt.io;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
 * 四个步骤:分段读取
 * 1.创建源:字节数组 不要太大 2.选择流 3.操作 4.释放资源:可以不用处理
 * @author 韩文韬
 *
 */
public class IOTest07 {
	
	public static void main(String[] args) {
		//1.创建源
		byte[] src="talk is cheap show me the code".getBytes();
		//2.选择流
		InputStream is=null;
		try {
			is=new ByteArrayInputStream(src);
			//3.操作(读取)
			byte[] flush=new byte[5];  //缓冲容器
			int len=-1;  //接受长度
			while((len=is.read(flush))!=-1)
			{
				//字符数组------->字符串
				String str=new String(flush,0,len);
				System.out.println(str);
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			//4.释放资源
			try {
				if(is!=null)
				{
					is.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
}

输出结果为:
talk
is ch
eap s
how m
e the
code
下面我们来看字节数组输出流
我们来看一下API:
ByteArrayOutputStream() 创建一个新的字节数组输出流;
由此可以看出,它不需要指定目的地,当数据写入缓冲区时,缓冲区会自动增长;

package com.sxt.io;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * 字节数组输出流 ByteArrayOutputStream
 * 1.创建源 :内部维护2.选择流:不关联源 3.操作(写出内容) 4.释放资源:可以不用
 * 
 * 获取数据 toByteArray
 * @author 韩文韬
 *
 */
public class IOTest08 {
	public static void main(String[] args) {
		//1.创建源
		byte[] dest=null;
		//2.选择流(新增方法)
		ByteArrayOutputStream baos=null;
		try {
			baos=new ByteArrayOutputStream();
			//3.操作(写出)
			String msg="show me the code";
			byte[] datas=msg.getBytes();  //字符串--->字符数组(编码)
			baos.write(datas, 0, datas.length);
			baos.flush();
			//获取数据
			dest=baos.toByteArray();
			System.out.println(dest.length+"--->"+new String(dest,0,baos.size()));
		}catch(FileNotFoundException e){
			e.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		}finally {
			//4.释放资源
			try {
				if(baos!=null)
				{
					baos.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}
}

输出结果为:
16—>show me the code
下面我们来思考一下:将文件转为字节数组,再将字节数组转为文件,可以实现吗?
在这里插入图片描述
下面用完整的代码来实现它!

package com.sxt.io;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * 1.图片读取到字节数组
 * 2.字节数组写出到文件
 * @author 韩文韬
 *
 */
public class IOTest09 {
	public static void main(String[] args) {
		//图片转成字节数组
		byte[] datas=fileToByteArray("p.png");
		System.out.println(datas.length);
		//字节数组转成图片
		ByteArrayTofile(datas,"p-byte.png");
	}
	
	/**
	 * 1.图片读取到字节数组
	 * (1).图片到程序 FileInputStream
	 * (2).程序到字节数组 ByteArrayOutputStream
	 */
	public static byte[] fileToByteArray(String filePath) {
		//1.创建源与目的地
		File src=new File(filePath);
		byte[] dest=null;
		//2.选择流
		InputStream is=null;
		ByteArrayOutputStream baos=null;
		try {
			is=new FileInputStream(src);
			baos=new ByteArrayOutputStream();
			//3.操作(分段读取)
			byte[] flush=new byte[1024*10]; //缓冲容器
			int len=-1; //接受长度
			while((len=is.read(flush))!=-1) 
			{
				baos.write(flush, 0, len);  //写出到字节数组中
			}
			baos.flush();
			return baos.toByteArray();
		}catch(FileNotFoundException e){
			e.printStackTrace();
		}catch(IOException e){
			e.printStackTrace();
		}finally {
			try {
				//4.释放
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	/**
	 * 2.字节数组写出到图片
	 * (1).字节数组到程序ByteArrayInputStream
	 * (2).程序到文件FileOutputStream
	 */
	public static void ByteArrayTofile(byte[] src,String filePath) {
		//1.创建源
		File dest=new File(filePath);
		//2.选择流
		InputStream is=null;
		OutputStream os=null;
		try {
			is=new ByteArrayInputStream(src);
			os=new FileOutputStream(dest);
			//3.操作
			byte[] flush=new byte[1024*10];
			int len=-1;
			while((len=is.read(flush))!=-1) {
				os.write(flush, 0, len);
			}
			os.flush();
		}catch(FileNotFoundException e){
			e.printStackTrace();
		}catch(IOException e){
			e.printStackTrace();
		}finally {
			//4.释放资源
			try {
				os.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值