• 关于SequenceInputStream


    两个流合并时:

    package stream.sequence;
    
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileInputStream;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.SequenceInputStream;
    
    /**
     * SequenceInputStream 表示其他输入流的逻辑串联。
     * 它从输入流的有序集合开始,并从第一个输入流开始读取,直到到达文件末尾;
     * 接着从第二个输入流读取,依次类推,直到到达包含的最后一个输入流的文件末尾为止。
     * 
     * @author 半步疯子
     *
     */
    public class SequenceInputStreamDemo01 {
    	
    	// SequenceInputStream(InputStream s1, InputStream s2) 
    	
    	public static void main(String[] args) throws IOException {
    		InputStream s1 = new FileInputStream("src/stream/sequence/SequenceInputStreamDemo01.java");
    		InputStream s2 = new FileInputStream("src/stream/sequence/SequenceInputStreamDemo02.java");
    		
    		SequenceInputStream sis = new SequenceInputStream(s1, s2);
    		
    		InputStreamReader isr = new InputStreamReader(sis);
    		BufferedReader br = new BufferedReader(isr);
    		
    		BufferedWriter bw = new BufferedWriter(new FileWriter("CopySequence.java"));
    		
    		String line = null;
    		while((line = br.readLine()) != null) {
    		// while(br.ready()) {  /* 为什么合并流之后,不能使用ready方法?结果只有S1 */
    			// line = br.readLine();
    			bw.write(line);
    			bw.newLine();
    			bw.flush();
    		}
    		s1.close();
    		s2.close();
    		br.close();
    		bw.close();
    		
    		/*
    		BufferedReader br = new BufferedReader(
    				new InputStreamReader(
    						new SequenceInputStream(
    								new FileInputStream("src/stream/sequence/SequenceInputStreamDemo01.java"), 
    								new FileInputStream("src/special/RandomAccessFileDemo02.java")
    								)
    						)
    				);
    		BufferedWriter bw = new BufferedWriter(new FileWriter("CopySequence.java"));
    		br.close();
    		bw.close();
    		*/
    	}
    }
    

    多个流的时候存放到Vector中后进行合并:

    package stream.sequence;
    
    import java.io.BufferedOutputStream;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.SequenceInputStream;
    import java.util.Vector;
    
    /**
     * SequenceInputStream 表示其他输入流的逻辑串联。
     * 它从输入流的有序集合开始,并从第一个输入流开始读取,直到到达文件末尾;
     * 接着从第二个输入流读取,依次类推,直到到达包含的最后一个输入流的文件末尾为止。
     * 
     * @author 半步疯子
     *
     */
    public class SequenceInputStreamDemo02 {
    	
    	// SequenceInputStream(Enumeration<? extends InputStream> e)
    	// 通过枚举类:进行多个流的合并
    	public static void main(String[] args) throws IOException {
    		InputStream s1 = new FileInputStream("src/stream/sequence/SequenceInputStreamDemo02.java");
    		InputStream s2 = new FileInputStream("src/stream/sequence/SequenceInputStreamDemo01.java");
    		InputStream s3 = new FileInputStream("src/stream/sequence/SequenceInputStreamDemo02.java");
    		
    		Vector<InputStream> v = new Vector<InputStream>();
    		v.addElement(s1);
    		v.addElement(s2);
    		v.addElement(s3);
    		SequenceInputStream sis = new SequenceInputStream(v.elements());
    		
    		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copySequence02.java"));
    		
    		byte[] buf = new byte[1024];
    		int len = buf.length;
    		while((len = sis.read(buf, 0, len)) != -1) {
    			bos.write(buf, 0, len);
    		}
    		
    		sis.close();
    		bos.close();
    	}
    }
    


  • 相关阅读:
    如何实现虚拟机(VirtualBox)中的Ubuntu与Windows XP间的数据共享
    linux中安装sqlmap
    LookupError: unknown encoding: cp65001解决办法
    共勉
    Linux中安装Nginx
    touch: cannot touch `/home/tomcat7/logs/catalina.out': Permission denied
    程序猿的十一条浮躁表现
    MySQL中varchar类型排序
    Gson和FastJson
    jquery-messager-消息提示
  • 原文地址:https://www.cnblogs.com/mzywucai/p/11053435.html
Copyright © 2020-2023  润新知