📜  Java I/O-SequenceInputStream

📅  最后修改于: 2020-09-27 02:16:20             🧑  作者: Mango

Java SequenceInputStream类

Java SequenceInputStream类用于从多个流中读取数据。它顺序读取数据(一一读取)。

Java SequenceInputStream类声明

让我们看一下Java.io.SequenceInputStream类的声明:

public class SequenceInputStream extends InputStream

SequenceInputStream类的构造方法

Constructor Description
SequenceInputStream(InputStream s1, InputStream s2) creates a new input stream by reading the data of two input stream in order, first s1 and then s2.
SequenceInputStream(Enumeration e) creates a new input stream by reading the data of an enumeration whose type is InputStream.

SequenceInputStream类的方法

Method Description
int read() It is used to read the next byte of data from the input stream.
int read(byte[] ary, int off, int len) It is used to read len bytes of data from the input stream into the array of bytes.
int available() It is used to return the maximum number of byte that can be read from an input stream.
void close() It is used to close the input stream.

Java SequenceInputStream示例

在此示例中,我们将打印两个文件testin.txt和testout.txt的数据。

package com.javatpoint;

import java.io.*;
class InputStreamExample {  
  public static void main(String args[])throws Exception{  
   FileInputStream input1=new FileInputStream("D:\\testin.txt");  
   FileInputStream input2=new FileInputStream("D:\\testout.txt");  
   SequenceInputStream inst=new SequenceInputStream(input1, input2);  
   int j;  
   while((j=inst.read())!=-1){  
    System.out.print((char)j);  
   }  
   inst.close();  
   input1.close();  
   input2.close();  
  }  
}  

在这里,我们假设您有两个文件:testin.txt和testout.txt,它们具有以下信息:

testin.txt:

Welcome to Java IO Programming.

testout.txt:

It is the example of Java SequenceInputStream class.

执行该程序后,您将获得以下输出:

输出:

Welcome to Java IO Programming. It is the example of Java SequenceInputStream class.

从两个文件读取数据并将其写入另一个文件的示例

在此示例中,我们将两个文件testin1.txt和testin2.txt的数据写入另一个名为testout.txt的文件中。

package com.javatpoint;

import java.io.*;  
class Input1{  
  public static void main(String args[])throws Exception{  
   FileInputStream fin1=new FileInputStream("D:\\testin1.txt");  
   FileInputStream fin2=new FileInputStream("D:\\testin2.txt");  
   FileOutputStream fout=new FileOutputStream("D:\\testout.txt");    
   SequenceInputStream sis=new SequenceInputStream(fin1,fin2);  
   int i;  
   while((i=sis.read())!=-1)  
   {  
     fout.write(i);      
   }  
   sis.close();  
   fout.close();    
   fin1.close();    
   fin2.close();     
   System.out.println("Success..");
  }  
}  

输出:

Succeess...

testout.txt:

Welcome to Java IO Programming. It is the example of Java SequenceInputStream class.

使用枚举读取数据的SequenceInputStream示例

如果需要从两个以上的文件中读取数据,则需要使用Enumeration。枚举对象可以通过调用Vector类的elements()方法获得。让我们看一个简单的示例,其中我们从4个文件中读取数据:a.txt,b.txt,c.txt和d.txt。

package com.javatpoint;
import java.io.*;  
import java.util.*;  
class Input2{  
public static void main(String args[])throws IOException{  
//creating the FileInputStream objects for all the files  
FileInputStream fin=new FileInputStream("D:\\a.txt");  
FileInputStream fin2=new FileInputStream("D:\\b.txt");  
FileInputStream fin3=new FileInputStream("D:\\c.txt");  
FileInputStream fin4=new FileInputStream("D:\\d.txt");  
//creating Vector object to all the stream  
Vector v=new Vector();  
v.add(fin);  
v.add(fin2);  
v.add(fin3);  
v.add(fin4);            
//creating enumeration object by calling the elements method  
Enumeration e=v.elements();    
//passing the enumeration object in the constructor  
SequenceInputStream bin=new SequenceInputStream(e);  
int i=0;    
while((i=bin.read())!=-1){  
System.out.print((char)i);  
}   
bin.close();  
fin.close();  
fin2.close();  
}  
}  

a.txt,b.txt,c.txt和d.txt具有以下信息:

a.txt:

Welcome

b.txt:

to

c.txt:

java

d.txt:

programming

输出:

Welcometojavaprogramming