SequenceInputStream的用法(用来合并流然后一起操作)

Posted windzly

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SequenceInputStream的用法(用来合并流然后一起操作)相关的知识,希望对你有一定的参考价值。

package test;
 
 
import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.zip.InflaterInputStream;
 
import javax.annotation.processing.FilerException;
import javax.management.RuntimeErrorException;

import privateclass.Filterby_Name;
import privateclass.Filterby_hidden;
import privateclass.Filterby_java;
import privateclass.MyBufferedReader;
 
public class Main {
 
    private static final String space_operator = " ";
    private static final double pi = Math.PI;
    private static final String LINE_SEPARATOR = System.getProperty("line.separator");
    public static void main(String[] args) throws Exception {
 
    	/*
    	 * 我们可以通过vector获取一个Enumeration的枚举对象
    	 
    	Vector<FileInputStream> vector = new Vector<FileInputStream>();
    	vector.add(new FileInputStream("1.txt"));
    	vector.add(new FileInputStream("2.txt"));
    	vector.add(new FileInputStream("3.txt"));
    	vector.add(new FileInputStream("4.txt"));
    	
    	Enumeration<FileInputStream> en = vector.elements();
    	*/
    	
    	/*
    	 *思考我们可不可以用list获取
    	 *
    	 */
    	ArrayList<FileInputStream> list = new ArrayList<FileInputStream>();
    	list.add(new FileInputStream("1.txt"));
    	list.add(new FileInputStream("2.txt"));
    	list.add(new FileInputStream("3.txt"));
    	list.add(new FileInputStream("4.txt"));
    	
    	//这就是用到迭代器原理来获取Enumeration
    	/*
    	Iterator<FileInputStream> it = list.iterator();
    	Enumeration<FileInputStream> en = new Enumeration<FileInputStream>(){

			@Override
			public boolean hasMoreElements() {
				// TODO Auto-generated method stub
				return it.hasNext();
			}

			@Override
			public FileInputStream nextElement() {
				// TODO Auto-generated method stub
				return it.next();
			}
    		
    	};
    	*/
    	
    	//直接用Collections里面封装的方法
    	Enumeration<FileInputStream> en = Collections.enumeration(list);
    	
    	SequenceInputStream sis = new SequenceInputStream(en);
    	
    	FileOutputStream fos = new FileOutputStream("7.txt");
    	int len = 0;
    	byte[] buf = new byte[1024];
    	while((len = sis.read(buf)) != -1)
    	{
    		fos.write(buf,0,len);
    	}
    	
    	fos.close();
    	sis.close();
    	
    	
    }
	
}

  

以上是关于SequenceInputStream的用法(用来合并流然后一起操作)的主要内容,如果未能解决你的问题,请参考以下文章

java入门了解12

合并流/SequenceInputStream

Java学习之IO流(序列流--SequenceInputStream)

java 合并流(SequenceInputStream)

java.io.SequenceInputStream

java从SequenceInputStream问题中读取内容