Java ByteArrayInputStream 类
Java ByteArrayInputStream 类
在本教程中,我们将通过示例了解 Java ByteArrayInputStream 及其方法。
ByteArrayInputStream
java.io
的类 包可用于读取输入数据数组(以字节为单位)。
它扩展了 InputStream
抽象类。
注意 :在 ByteArrayInputStream
,输入流是使用字节数组创建的。它包括一个内部数组来存储该特定字节数组的数据。
创建一个 ByteArrayInputStream
为了创建字节数组输入流,我们必须导入 java.io.ByteArrayInputStream
先打包。导入包后,我们可以通过以下方式创建输入流。
// Creates a ByteArrayInputStream that reads entire array
ByteArrayInputStream input = new ByteArrayInputStream(byte[] arr);
在这里,我们创建了一个输入流,它从 arr
读取整个数据 大批。但是,我们也可以创建只从数组中读取部分数据的输入流。
// Creates a ByteArrayInputStream that reads a portion of array
ByteArrayInputStream input = new ByteArrayInputStream(byte[] arr, int start, int length);
这里输入流读取的字节数等于 length 从 start 开始的数组 位置。
ByteArrayInputStream的方法
ByteArrayInputStream
类为 InputStream
中存在的不同方法提供实现 类。
read() 方法
read()
- 从输入流中存在的数组中读取单个字节read(byte[] array)
- 从输入流中读取字节并存储在指定的数组中read(byte[] array, int start, int length)
- 读取等于 length 的字节数 来自流并存储在从位置 start 开始的指定数组中
示例:ByteArrayInputStream 读取数据
import java.io.ByteArrayInputStream;
public class Main {
public static void main(String[] args) {
// Creates an array of byte
byte[] array = {1, 2, 3, 4};
try {
ByteArrayInputStream input = new ByteArrayInputStream(array);
System.out.print("The bytes read from the input stream: ");
for(int i= 0; i < array.length; i++) {
// Reads the bytes
int data = input.read();
System.out.print(data + ", ");
}
input.close();
}
catch(Exception e) {
e.getStackTrace();
}
}
}
输出
The bytes read from the input stream: 1, 2, 3, 4,
在上面的示例中,我们创建了一个名为 input
的字节数组输入流 .
ByteArrayInputStream input = new ByteArrayInputStream(array);
在这里,输入流包括来自指定数组的所有数据。为了从输入流中读取数据,我们使用了 read()
方法。
available() 方法
要获取输入流中的可用字节数,我们可以使用 available()
方法。例如,
import java.io.ByteArrayInputStream;
public class Main {
public static void main(String args[]) {
// Creates an array of bytes
byte[] array = { 1, 2, 3, 4 };
try {
ByteArrayInputStream input = new ByteArrayInputStream(array);
// Returns the available number of bytes
System.out.println("Available bytes at the beginning: " + input.available());
// Reads 2 bytes from the input stream
input.read();
input.read();
// Returns the available number of bytes
System.out.println("Available bytes at the end: " + input.available());
input.close();
}
catch (Exception e) {
e.getStackTrace();
}
}
}
输出
Available bytes at the beginning: 4 Available bytes at the end: 2
在上面的例子中,
- 我们使用了
available()
检查输入流中可用字节数的方法。 - 然后我们使用了
read()
方法 2 次从输入流中读取 2 个字节。 - 现在,在读取 2 个字节后,我们检查了可用字节。这次可用字节减少了 2。
skip() 方法
要丢弃和跳过指定的字节数,我们可以使用 skip()
方法。例如,
import java.io.ByteArrayInputStream;
public class Main {
public static void main(String args[]) {
// Create an array of bytes
byte[] array = { 1, 2, 3, 4 };
try {
ByteArrayInputStream input = new ByteArrayInputStream(array);
// Using the skip() method
input.skip(2);
System.out.print("Input stream after skipping 2 bytes: ");
int data = input.read();
while (data != -1) {
System.out.print(data + ", ");
data = input.read();
}
// close() method
input.close();
}
catch (Exception e) {
e.getStackTrace();
}
}
}
输出
Input stream after skipping 2 bytes: 3, 4,
在上面的例子中,我们使用了 skip() 方法从输入流中跳过 2 个字节的数据。因此 1 和 2 不会从输入流中读取。
close() 方法
要关闭输入流,我们可以使用 close()
方法。
但是,close()
ByteArrayInputStream
中的方法无效 班级。即使在 close()
之后我们也可以使用这个类的方法 方法被调用。
ByteArrayInputStream的其他方法
方法 | 说明 |
---|---|
finalize() | 确保 close() 方法被调用 |
mark() | 标记输入流中已读取数据的位置 |
reset() | 将控件返回到输入流中设置标记的点 |
markSupported() | 检查输入流是否支持mark() 和 reset() |
要了解更多信息,请访问 Java ByteArrayInputStream(Java 官方文档)。
java