摘要:
下文使用Java代码中ByteArrayInputStream和ByteArrayOutputStream类的用法分享,如下所示:
ByteArrayInputStream和ByteArrayOutputStream类简介:
ByteArrayInputStream:从内存中的字节数组读取数据
ByteArrayOutputStream:将数据写入内存中的字节数组
例:
Java 中ByteArrayInputStream和ByteArrayOutputStream类的示例
ByteArrayInputStream示例
package com.maomao365;
import java.io.ByteArrayInputStream;
/**
* @author maomao365
*/
public class ByteArrayInputStreamExample {
public static void main(String[] args) {
String data="the website name is maomao365.com";
/*获取内存中变量*/
ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(data.getBytes());
int d;
while((d=byteArrayInputStream.read())!=-1){
System.out.print((char)d);
}
}
}
//输出
the website name is maomao365.com
ByteArrayOutputStream的示例
package com.maomao365;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
/**
* @author maomao365
*/
public class ByteArrayOutputStreamExample {
public static void main(String[] args) throws IOException {
String string1 = "the info1 ";
String string2 = " the info2";
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byteArrayOutputStream.write(string1.getBytes());
byteArrayOutputStream.write(string2.getBytes());
System.out.println("Output data : " + byteArrayOutputStream.toString());
}
}
//输出
the info1 the info2