Java.io.SequenceInputStream.read() 方法
描述
java.io.SequenceInputStream.read() 方法从此输入流中读取数据的下一个字节。 字节作为 int 返回,范围为 0 到 255。如果由于到达流的末尾而没有可用的字节,则返回值 -1。 此方法会一直阻塞,直到输入数据可用、检测到流结束或引发异常。
声明
以下是 java.io.SequenceInputStream.read() 方法的声明。
public int read()
参数
NA
返回值
此方法返回数据的下一个字节,如果到达流的末尾,则返回 -1。
异常
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);
try {
// read 10 characters, 5 from each stream
for (int i = 0; i < 10; i++) {
char c = (char) sis.read();
System.out.print("" + c);
}
// change line
System.out.println();
// close the streams
sis.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
HelloWorld

