字节数组流不用关闭,但是关了也没关系
ByteArrayInputStream
public static void main(String[] args) {
toBy("asdasdasdasd".getBytes());
}
public static void toBy(byte[] by) {
//1创建源 byte[] by
//2选择流
InputStream is = null;
is = new ByteArrayInputStream(by);
//3操作
try {
byte[] flush = new byte[5];
int len =-1;
while((len = is.read(flush))!=-1) {
String str = new String(flush, 0, len);
System.out.println(str);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//4
ByteArrayOutputStream
*字节数组输出流
* @author
*1创建源.不需要指定目的地,内部创建
*2选择流:因为要使用toByteArray(),是子类的方法,所以不需要关联源
*3操作 写出内容
*4释放资源:可以不需要
*5获取数据:toByteArray()方法
*
*/
public class TestByteArrayOut {
public static void main(String[] args) {
toByteOut("88888888");
}
public static void toByteOut(String str) {
//1创建一个空数组接收数据
byte[] by = null;
//2选择流
ByteArrayOutputStream baos = null;
//3操作
baos = new ByteArrayOutputStream();
byte[] datas = str.getBytes();
baos.write(datas,0,datas.length);
by = baos.toByteArray();
System.out.println(by.length+"-->"+new String(by, 0, baos.size()));
try {
baos.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}