public class test{
public static void main(String args[]) throws Exception {
ByteArrayOutputStream output = new ByteArrayOutputStream();
String str = "hello,world";
output.write(str.getBytes()); //数据存储在在output里
//将数据写入到内存中
//InputStream input = new ByteArrayInputStream(output.toByteArray());
InputStream input = new ByteArrayInputStream("hello,world".getBytes());
int temp = 0;
while((temp=input.read())!=-1){ //从数据内存中读取数据
System.out.println((char)temp);
}
}
}
本文通过一个简单的Java程序示例介绍了如何使用字节输出流 ByteArrayOutputStream 和字节输入流 ByteArrayInputStream 进行数据的写入与读取。该示例展示了字符串转换为字节数组并进行读写的完整过程。
6364

被折叠的 条评论
为什么被折叠?



