SequenceInputStream类允许连接多个InputStreams。它逐个读取流的数据。它从输入流的有序集合开始,从第一个流开始读取,直到到达文件末尾,然后从第二个流开始读取,依此类推,直到最后一个包含的输入流到达文件末尾。
null
构造函数和描述
- SequenceInputStream(枚举e): 初始化新创建的SequenceInputStream,它必须是生成类型为InputStream的对象的枚举。
- SequenceInputStream(InputStream s1,InputStream s2): 通过记住两个参数来初始化新创建的SequenceInputStream,这两个参数将按顺序读取,首先是s1,然后是s2。
重要方法:
- 阅读: 从这个输入流中读取下一个字节的数据。
Syntax: public int read() throws IOException Returns: the next byte of data, or -1 if the end of the stream is reached. Throws: IOException - if an I/O error occurs.
- 读取(字节[]b,整数关闭,整数长度): 从这个输入流中读取多达len字节的数据到
Syntax: public int read(byte[] b, int off, int len) throws IOException Overrides: read in class InputStream Parameters: b - the buffer into which the data is read. off - the start offset in array b at which the data is written. len - the maximum number of bytes read. Returns: int the number of bytes read. Throws: NullPointerException - If b is null. IndexOutOfBoundsException - If off is negative, len is negative, or len is greater than b.length - off IOException - if an I/O error occurs.
- 可供选择: 返回可从当前基础输入流读取(或跳过)的字节数的估计值,而无需在下次调用当前基础输入流的方法时阻塞。
Syntax : public int available() throws IOException Overrides: available in class InputStream Returns:an estimate of the number of bytes that can be read (or skipped over) from the current underlying input stream without blocking or 0 if this input stream has been closed by invoking its close() method Throws: IOException - if an I/O error occurs.
- 关闭: 关闭此输入流并释放与该流关联的所有系统资源。
Syntax : public void close() throws IOException Overrides: close in class InputStream Throws: IOException - if an I/O error occurs.
下面是SequenceInputStream类的一个示例,它实现了一些重要的方法。 节目:
//Java program to demonstrate SequenceInputStream import java.io.*; import java.util.*; class SequenceISDemp { public static void main(String args[]) throws IOException { //creating the FileInputStream objects for all the following files FileInputStream fin= new FileInputStream( "file1.txt" ); FileInputStream fin2= new FileInputStream( "file2.txt" ); FileInputStream fin3= new FileInputStream( "file3.txt" ); //adding fileinputstream obj to a vector object Vector v = new Vector(); v.add(fin); v.add(fin2); v.add(fin3); //creating enumeration object by calling the elements method Enumeration enumeration = v.elements(); //passing the enumeration object in the constructor SequenceInputStream sin = new SequenceInputStream(enumeration); // determine how many bytes are available in the first stream System.out.println( "" + sin.available()); // Estimating the number of bytes that can be read // from the current underlying input stream System.out.println( sin.available()); int i = 0 ; while ((i = sin.read())! = - 1 ) { System.out.print(( char )i); } sin.close(); fin.close(); fin2.close(); fin3.close(); } } |
输出:
19 This is first file This is second file This is third file
注意:此程序不会在联机IDE上运行,因为没有与之关联的文件。
本文由 尼森特·夏尔马 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END