ByteArrayInputStream
包含一个内部缓冲区,该缓冲区存储从流中读取的字节。内部计数器跟踪 read
方法要提供的下一个字节。
关闭 ByteArrayInputStream 无效。此类中的方法在关闭此流后仍可被调用,而不会产生任何 IOException。
ByteArrayOutputStream 此类实现了一个输出流,其中的数据被写入一个字节数组。缓冲区会随着数据的不断写入而自动增长。可使用 toByteArray()
和 toString()
检索数据。
关闭 ByteArrayOutputStream 无效。在关闭此流后且没有生成 IOException 时,可以调用此类中的该方法
@Test
public void test2() {
byte[] buf = "abcdefghijklmnopqrstuvwxyz".getBytes();
//像输入流中输入一段信息
ByteArrayInputStream in = new ByteArrayInputStream(buf);
ByteArrayOutputStream out = new ByteArrayOutputStream();
transform(in, out);
//转换成数组,然后组装成字符串打印
System.out.println(new String(out.toByteArray()));
}
private void transform(InputStream in,OutputStream out){
try {
int ch= 0;
while((ch=in.read())!=-1){
int upper = Character.toUpperCase((char)ch);
out.write(upper);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(out!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(in!=null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}