我们经常使用I/O流来读写文件内容,除了文件之外,I/O流同样可以用来操作内存,这种流被称为内存操作流
内存操作流的好处:进行I/O操作,不会产生文件
内存流根据操作的数据类型可分为两类:
1.字节内存流:ByteArrayInputStream、ByteArrayOutputStream
2.字符内存流:CharArrayReader、CharArrayWriter
内存流继承关系
范例:通过内存流实现大小写转换
public class Test{
public static void main(String[]args) throws IOException {
String message="hello word";
// 实例化InputStream类对象,实例化的时候需要将你操作的数据保存到内存之中
// 最终读取的就是你设置的内容。
InputStream input = new ByteArrayInputStream(message.getBytes()) ;
//ByteArrayInputStream类的构造方法要求传入一个字节数组
OutputStream output=new ByteArrayOutputStream();
int temp = 0 ;
while((temp = input.read()) != -1) {
// 每个字节进处理,处理之后所有数据都在outputStream类中
output.write(Character.toUpperCase(temp)) ;
}
// 直接输出output对象
System.out.println(output) ;
input.close() ;
output.close() ;
}
}
从以上范例中可以看出,内存流操作并不会产生文件,可以理解为临时文件处理