Java.io.SequenceInputStream.read() 方法
描述
java.io.SequenceInputStream.read(byte[] b,int off,int len) 方法从这个输入流中读取最多 len 个字节的数据到一个字节数组中。 如果 len 不为零,则该方法会阻塞,直到至少有 1 个字节的输入可用; 否则,不读取任何字节并返回 0。
声明
以下是 java.io.SequenceInputStream.read() 方法的声明。
public int read(byte[] b,int off,int len)
参数
b − 读取数据的缓冲区。
off − 数组 b 中写入数据的起始偏移量。
b − 读取的最大字节数。
返回值
该方法返回读取的字节数。
异常
NullPointerException − 如果 b 为空
IndexOutOfBoundsException − 如果 off 为负数,len 为负数,或者 len 大于 b.length - off
IOException − 如果发生 I/O 错误。
示例
下面的例子展示了 java.io.SequenceInputStream.read() 方法的使用。
package com.tutorialspoint;
import java.io.*;
public class SequenceInputStreamDemo {
public static void main(String[] args) {
// create two new strings with 5 characters each
String s1 = "Hello";
String s2 = "World";
// create 2 input streams
byte[] b1 = s1.getBytes();
byte[] b2 = s2.getBytes();
ByteArrayInputStream is1 = new ByteArrayInputStream(b1);
ByteArrayInputStream is2 = new ByteArrayInputStream(b2);
// create a new Sequence Input Stream
SequenceInputStream sis = new SequenceInputStream(is1, is2);
// create a new byte array
byte arr[] = {'1', '2', '3', '4'};
try {
// read 3 chars and print the number of chars read
System.out.print("" + sis.read(arr, 0, 3));
// change line
System.out.println();
// close the streams
sis.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
3

