bytearrayOutputstream
时间: 2023-10-21 22:04:03 浏览: 97
ByteArrayOutputStream is a class in Java that provides an output stream for writing data to a byte array. It extends the OutputStream class and provides additional methods for writing data to a byte array.
The ByteArrayOutputStream class maintains an internal buffer that automatically grows as data is written to it. The data can be retrieved from the buffer using the toByteArray() or toString() method.
Example Usage:
```java
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
outputStream.write("Hello world!".getBytes());
byte[] byteArray = outputStream.toByteArray();
String string = outputStream.toString();
```
In the above example, a new ByteArrayOutputStream object is created and the string "Hello world!" is written to it using the write() method. The data is then retrieved from the buffer using the toByteArray() and toString() methods.
阅读全文