package io02;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
/**
- 字节数组输出流 ByteArrayOutputStream
- 1.创建源:内部维护
- 2.选择流:不关联源
- 3.操作
- 4.释放资源 可以不用
- 获取数据:toByteArray()
- @author
*/
public class TestIO09 {
public static void main(String[] args) {
//1.创建源:不需要创建源
byte[] dest = null;
//2.选择流 (新增方法)
ByteArrayOutputStream baos = null;
try {
baos = new ByteArrayOutputStream();
//3.操作(写出)
String msg = "talk is cheap show me code";
byte[] temp = msg.getBytes();
baos.write(temp,0,temp.length);
baos.flush();
//获取数据
dest = baos.toByteArray();
System.out.println(dest.length+"-->"+new String(dest,0,baos.size()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}