字节数组流

优势 及要点
1.任何数据都可以处理成二进制数组, 方便后期网络上的传输 。
2.源头是一块内存而不是文件,java虚拟机可以直接访问
3.因为源头是内存,所以数据不能太大
4不用释放,java虚拟机会自动释放。

字节数组输入流

package 字节数组流;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

public class 字节数组读取流 {
public static void main(String[] args) {
	//1.选择源
	byte [] a= "talk the cheap show me the code".getBytes();
	//2.选择流
	InputStream in = null;
	in = new ByteArrayInputStream(a);
	//3.操作
	byte [] flush = new byte [128];
	int len =-1;
	try {
		while((len=in.read(flush))!=-1) {
			String str =new String(flush,0,flush.length);
			System.out.println(str);
		}
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	//4.释放资源  为了形式统一,可以不用
	try {
		in.close();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
}

字节数组输出流

package 字节数组流;

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

public class 字节数组输出流 {
public static void main(String[] args) {
	//选择源
		byte [] file  =null;//不需要源
		//选择流 (因为子类有新的方法 所以不能再用父类)
		ByteArrayOutputStream output =null;
		try {
			output = new ByteArrayOutputStream();
			//操作
			String s ="talk is cheap show me the code";
			byte [] n=s.getBytes();//编码
			output.write(n, 0, n.length);
			output.flush();//刷新
			//获取数据
			file=output.toByteArray();
			System.out.println(new String(file ,0 ,file.length));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {//关闭流
			if(null!=output) {
				try {
					output.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
}
}

综合对接流

package 综合起来的;

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;

public class 综合_对接流 {//文件——程序——字节数组 (文件到字节数组)
	                  //字节数组——程序——文件(字节数组到文件)
public static void main(String[] args) {

	
	arrayfile(filearray("1.jpg"), "2.jpg");
	
}
public static byte[] filearray(String path) {//文件到字节数组
	File file = new File(path);
	byte [] date =null;
	//选择流
	InputStream in= null;
	ByteArrayOutputStream inarray= null;
	try {
	in = new FileInputStream(file);
	inarray =new ByteArrayOutputStream();
	//操作(分段读取)
		byte [] flush =new byte [128];//缓冲容器
		int len = -1;//接收容器
		while((len = in.read(flush))!=-1) {
			inarray.write(flush,0, len);
		}
		
		
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally {//释放资源
		
		if(null!=in) {
			try {
				in.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	return inarray.toByteArray();//将字节数组返回
	
}
public static void  arrayfile(byte []array,String path) {//字节数组到文件
	//选择源
		File file  =new File(path);
		
		//选择流
		OutputStream output =null;
		InputStream arrayin = null;
		try {
			output = new FileOutputStream(file );
			//操作
			arrayin =new  ByteArrayInputStream(array);
			output.write(array, 0, array.length);
			output.flush();//刷新
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {//关闭流
			if(null!=output) {
				try {
					output.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
}
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值