1、ByteArrayInputStream
ByteArrayInputStream 继承自 InputStream
-
包含一个内部缓冲区,其中包含可以从流中读取的字节。 内部计数器跟踪
read
方法要提供的下一个字节。 -
关闭ByteArrayInputStream没有任何效果(调用close方法)。 在关闭流之后,可以调用此类中的方法,而不生成IOException 。
- 使用和InputStream差不多一样,InputStream传入的是File路径,而ByteArrayInputStream传入的则是byte数组,使用如下。
ByteArrayInputStream inputStream = null;
String str = "测试ByteArrayInputStream";
byte[] bytes = str.getBytes();
inputStream = new ByteArrayInputStream(bytes);
byte[] bytes1 = new byte[1024];
int len;
while ((len = inputStream.read(bytes1)) != -1) {
String s = new String(bytes1, 0, bytes1.length);
System.out.println(s);
}
//close方法只是写了方法名,没有具体的关闭操作,不会抛出IOException
inputStream.close();
2、ByteArrayOutputStream
ByteArrayOutputStream 继承自outputStream
-
实现了将数据写入字节数组的输出流。 当数据写入缓冲区时,缓冲区会自动增长。 数据可以使用
toByteArray()
和toString()
。 -
关闭ByteArrayOutputStream没有任何效果。 该流中的方法可以在流关闭后调用,而不生成IOException 。
close() | 关闭 ByteArrayOutputStream没有任何效果。 |
size() | 返回缓冲区的当前大小。 |
toByteArray() | 创建一个新分配的字节数组。 |
toString() | 使用平台的默认字符集将缓冲区内容转换为字符串解码字节。 |
toString(String charsetName) | 通过使用命名的charset解码字节,将缓冲区的内容转换为字符串。 |
write(byte[] b, int off, int len) | 从指定的字节数组写入 len字节,从偏移量为 off开始,输出到这个字节数组输出流。 |
write(int b) | 将指定的字节写入此字节数组输出流。 |
writeTo(OutputStream out) | 将此字节数组输出流的完整内容写入指定的输出流参数,就像使用 out.write(buf, 0, count)调用输出流的写入方法 out.write(buf, 0, count) 。 |
reset() | 将此字节数组输出流的 count字段重置为零,以便丢弃输出流中当前累积的所有输出。 |
write(int b) 、write(byte[] b, int off, int len) 、 reset() 、size() 、toByteArray() 使用如下:
public void test1() {
byte[] bytes = "defghijk".getBytes();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
outputStream.write(97);
outputStream.write(98);
outputStream.write(99);
// 截取数组
outputStream.write(bytes, 0, 5);
// 获取数组长度
int size = outputStream.size();
// 获取当前截取后的数组
byte[] toByteArray = outputStream.toByteArray();
// 输出数组长度
System.out.println(size);
// 输出当前数组
System.out.println(Arrays.toString(toByteArray));
System.out.println("##########重置后");
// reset 相当于清空数据
outputStream.reset();
// 输出数组长度
System.out.println(outputStream.size());
// 输出当前数组
System.out.println(Arrays.toString(outputStream.toByteArray()));
}
toString(String charsetName) 、writeTo(OutputStream out) 、close()
public void test2() throws IOException {
byte[] bytes="defghijk".getBytes("GBK");
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
outputStream.write(bytes);
//如果 编码与解码的格式不同意就会出现乱码现象
String s = outputStream.toString("GBK");
System.out.println(s);
//和ByteArrayInputStream 的close方法一样
outputStream.close();
//将输出流的数据写入到另一个流中
ByteArrayOutputStream out = new ByteArrayOutputStream();
outputStream.writeTo(out);
//输出查看
System.out.println(Arrays.toString(out.toByteArray()));
}
3、ByteArrayOutputStream与ByteArrayInputStream联合使用:
public static void main(String[] args) {
try {
read(write());
} catch (IOException e) {
e.printStackTrace();
}
}
public static byte[] write() throws IOException {
ByteArrayOutputStream outputStream=null;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
String str = "测试ByteArrayOutputStream与ByteArrayInputStream";
byte[] bytes = str.getBytes();
stream.write(bytes);
return stream.toByteArray();
}
public static void read(byte [] bytes) throws IOException {
ByteArrayInputStream inputStream = null;
inputStream = new ByteArrayInputStream(bytes);
byte[] bytes1 = new byte[1024];
int len;
while ((len = inputStream.read(bytes1)) != -1) {
String s = new String(bytes1, 0, bytes1.length);
System.out.println(s);
}
//close方法只是写了方法名,没有具体的关闭操作,不会抛出IOException
inputStream.close();
}