ByteArrayInputStream
时间: 2023-10-22 13:19:12 浏览: 83
ByteArrayInputStream is a class in Java that reads bytes from a byte array. It allows you to read a byte array as an input stream. The data is read from the byte array in a sequential manner. This class is useful when you have a byte array that you want to read as an input stream.
The constructor of ByteArrayInputStream takes a byte array as an argument. The class has several methods to read the data from the byte array. For example, you can use the read() method to read a single byte from the input stream, or the read(byte[] b, int off, int len) method to read a specified number of bytes into a byte array.
Here is an example of using ByteArrayInputStream:
```
byte[] bytes = {1, 2, 3, 4, 5};
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
int data = inputStream.read();
while (data != -1) {
System.out.print(data + " ");
data = inputStream.read();
}
```
This code creates a byte array with the values 1, 2, 3, 4, and 5. Then it creates a new ByteArrayInputStream object with the byte array as the input. The code then reads the data from the input stream using the read() method and prints it to the console. The output of this code would be:
```
1 2 3 4 5
```
阅读全文