字节内存流 | OutputStream{abstract} | | InputStream{abstract} | | | :—-: | :—-: | —- | —- | | FileOutputStream | ByteArrayOutputStream | FileInputStream | ByteArrayInputStream |
字符内存流 | Writer{abstract} | | Reader{abstract} | | | :—-: | :—-: | —- | —- | | OutputStreamWriter | CharArrayWriter | InputStreamReader | CharArrayReader | | FileWriter | | FileReader | |
- 字节内存流和字符内存流唯一的区别在于操作数据类型
内存流的关闭是无效的
构造函数(以字节内存流为例)
public ByteArrayOutputStream() 内存输出流,从内存向程序( 内存)输出
public ByteArrayInputStream(bytes[] buf) 内存输入流,向内存输入
程序也是在内存中的,实质上完全可以自己用byte[]来实现内存流
String string="zhou_xv_de_string";
//字节内存输出流,其write函数是向对象内的 byte buf[] 中写入
ByteArrayOutputStream bao=new ByteArrayOutputStream();
//字节内存输入流,构造时传入字节数组作为内存缓冲区,read函数从这个缓冲数组中读取
ByteArrayInputStream bai=new ByteArrayInputStream(string.getBytes(StandardCharsets.UTF_8));
int temp;
while((temp=bai.read())!=-1){
bao.write(Character.toUpperCase(temp));
}
System.out.println(bao);
bao.close();
bai.close();