内存操作流
内存操作流:用于处理临时存储信息的,程序结束,数据就从内存中消失。
名称规则: 以字节结尾的必须使用字节输出流结尾(in/out putStream)否则使用字符。
字节数组:
- ByteArrayInputStream
- ByteArrayOutputStream
- ==用法==
public class ByteArrayStreamDemo {
public static void main(String[] args) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
for (int x = 0; x < 10; x++) {
baos.write(("hello" + x).getBytes());
}
byte[] bys = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(bys);
int by = 0;
while ((by = bais.read()) != -1) {
System.out.print((char) by);
}
}
}
字符数组
- CharArrayReader
- CharArrayWriter
public static void main(String[] args) throws IOException {
CharArrayWriter cw=new CharArrayWriter();
cw.write("hello".toCharArray());
char[] a=cw.toCharArray();
System.out.println(a);
CharArrayReader cr=new CharArrayReader(a);
char[] buf=new char[1024];
int len;
while((len=cr.read(buf))!=-1){
System.out.print(new String(buf,0,len));
}
}
字符串
public static void main(String[] args) throws IOException {
String first="hello";
StringWriter sw=new StringWriter();
sw.write("hello");
StringReader sr=new StringReader("hehe");
char[] buf=new char[1024];
int len;
while((len=sr.read(buf))!=-1){
System.out.print(new String(buf,0,len));
}
}