一:内存流
1、内存流主要用来操作内存:
ByteArrayInputStream和ByteArrayOutputStream
2、输入和输入可以把文件作为数据源,也可以把内存作为数据源。
ByteArrayInputStream主要完成将内容从内存读入程序之中
ByteArrayOutputStream的功能主要是将数据写入到内存中。
注意:因为这两个流没有使用系统资源,所有不用关闭,也不需要抛出异常.
例题:读取内存中的内容
package memory;
import java.io.ByteArrayInputStream;
import java.io.IOException;
public class ByteArrayInputStreamDemo {
public static void main(String[] args) {
String data="你好,内存流";
ByteArrayInputStream bis=new ByteArrayInputStream(data.getBytes()); // 构造方法中传入的字节数组就是要读取的字节数组
byte[] b=new byte[1024]; // 自定义缓冲区
try {
int len=bis.read(b);
System.out.println("读取的内容是:"+new String(b,0,len));
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行结果: