当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


Java Java.io.SequenceInputStream用法及代码示例

SequenceInputStream 类允许您连接多个InputStream。它一一读取流的数据。它从输入流的有序集合开始,从第一个输入流读取,直到到达文件末尾,然后从第二个输入流读取,依此类推,直到最后一个包含的输入流到达文件末尾。

构造函数和说明

  • 序列输入流(枚举e):初始化一个新创建的SequenceInputStream,它必须是一个生成类型为InputStream的对象的Enumeration。
  • 序列输入流(输入流 s1,InputStream s2):通过记住两个参数来初始化新创建的SequenceInputStream,这两个参数将按顺序读取,首先是 s1,然后是 s2。

重要方法:

  • read :从此输入流读取下一个数据字节。
    用法:
    public int read()
             throws IOException 
    返回: the next byte of data, or -1 if the end of the stream is reached.
    Throws: IOException - if an I/O error occurs.
    
  • 读取(字节[] b,int关闭,int长度):从此输入流中读取最多 len 个字节的数据到数组中
    用法:
    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.
  • available :返回可以从当前基础输入流中读取(或跳过)的字节数的估计值,而不会被当前基础输入流的方法的下一次调用所阻塞。
    Syntax :
    public int available()
                  throws IOException 
    Overrides: available in class InputStream
    返回: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.
  • close:关闭此输入流并释放与该流关联的所有系统资源。
    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 上运行,因为没有与其关联的文件。



相关用法


注:本文由纯净天空筛选整理自佚名大神的英文原创作品 Java.io.SequenceInputStream in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。