例子
以下示例显示java.io.SequenceInputStream.available()方法的用法。
package com.jc2182;
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 {
// determine how many bytes are available in the first stream
System.out.println("" + sis.available());
// read 10 characters, 5 from each stream
for (int i = 0; i < 10; i++) {
char c = (char) sis.read();
System.out.print("" + c);
}
// close the stream
sis.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
让我们编译并运行以上程序,这将产生以下结果-
5
HelloWorld