【内存操作流(处理临时信息)】

package test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * @author shusheng
 * @description 内存操作流(处理临时信息)
 * @Email shusheng@yiji.com
 * @date 2018/12/23 2:35
 */
public class ByteArrayStreamDemo {
    /**
     * 内存操作流:用于处理临时存储信息的,程序结束,数据就从内存中消失。操作字节数组
     * ByteArrayInputStream ByteArrayOutputStream
     * 操作字符数组
     * CharArrayReader CharArrayWrite
     * 操作字符串
     * StringReader StringWriter
     */
    public static void main(String[] args) throws IOException {

        // 写数据 ByteArrayOutputStream()
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        // 写数据
        for (int x = 0; x < 10; x++) {
            baos.write(("hello" + x + "
").getBytes());
        }

        // public byte[] toByteArray()
        byte[] bys = baos.toByteArray();

        // 读数据
        // ByteArrayInputStream(byte[] buf)
        ByteArrayInputStream bais = new ByteArrayInputStream(bys);

        int by = 0;
        while ((by = bais.read()) != -1) {
            System.out.print((char) by);
        }
    }

}
终身学习者
原文地址:https://www.cnblogs.com/zuixinxian/p/10340675.html