📜  Java.io.SequenceInputStream 在Java中

📅  最后修改于: 2022-05-13 01:55:04.905000             🧑  作者: Mango

Java.io.SequenceInputStream 在Java中

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

构造函数和描述

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

重要方法:

  • read :从此输入流中读取数据的下一个字节。
    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.
    
  • read(byte[] b, int off, int len) :从此输入流中读取最多 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.
  • available :返回对当前底层输入流可以读取(或跳过)的字节数的估计值,而不会被当前底层输入流的方法的下一次调用阻塞。
    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.
  • 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 上运行,因为没有与之关联的文件。