1 package Iotext;
2 import java.io.BufferedInputStream;
3 import java.io.ByteArrayInputStream;
4 import java.io.ByteArrayOutputStream;
5 import java.io.IOException;
6 import java.io.InputStream;
7
8
9 /**
10 * 字节数组 节点流
11 * 数组的长度有限,数量不会很大
12 *
13 * 文件内容不用太大
14 * 1.文件---程序——》字节数组
15 * 2.字节数组——》程序——》文件
16 * @author Administrator
17 *
18 */
19 public class ByteArrayDemo01{
20 public static void main(String[] args) throws IOException{
21 read(write());
22 }
23 //将字符串传入字节数组
24 public static byte[] write() throws IOException{
25 byte[] dest;
26 ByteArrayOutputStream bos=new ByteArrayOutputStream();
27 String msg = "时光一去不复返,我们即将马踏四方";//将字符串存入字节数组
28 byte[] info=msg.getBytes();//字符串转换为字节数组
29 bos.write(info,0,info.length);
30 //获取数组
31 dest=bos.toByteArray();
32 //释放资源
33 bos.close();
34 return dest;
35 }
36
37
38 /**
39 * 输入流 操所与 文件输入流操作一致
40 * 读取字节数组
41 *
42 */
43 //将字节数组中的数据读出
44 public static void read(byte[] src) throws IOException{
45 InputStream is=new BufferedInputStream(new ByteArrayInputStream(src));
46 byte[] flush = new byte[1024];
47 int len=0;
48 while(-1 !=(len=is.read(flush))){
49 System.out.println(new String(flush,0,len));
50 }
51 is.close();
52 }
53 }