黑马程序员——Java IO—字节流—SequenceInputStream
2014-10-04 07:03
429 查看
----------------------
ASP.Net+Unity开发、.Net培训、期待与您交流! ----------------------
SequenceInputStream用于在逻辑上将多个输入流合并为一个输入流,从而可以连续读取多个输入流中的内容。当一个输入流中的数据被读取完时,SequenceInputStream会自动从下一个流中读取数据,直至所有的流全部读取完。
构造器:
SequenceInputStream没有提供额外的读取方法,所有方法都继承自InputStream,只是重写了一部分方法。
最常用的是3个重载的read方法。
小示例:
----------------------
ASP.Net+Unity开发、.Net培训、期待与您交流! ----------------------
ASP.Net+Unity开发、.Net培训、期待与您交流! ----------------------
SequenceInputStream用于在逻辑上将多个输入流合并为一个输入流,从而可以连续读取多个输入流中的内容。当一个输入流中的数据被读取完时,SequenceInputStream会自动从下一个流中读取数据,直至所有的流全部读取完。
构造器:
public SequenceInputStream(Enumeration<? extends InputStream> e) public SequenceInputStream(InputStream s1, InputStream s2)
SequenceInputStream没有提供额外的读取方法,所有方法都继承自InputStream,只是重写了一部分方法。
最常用的是3个重载的read方法。
小示例:
package org.lgy.study.io; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.SequenceInputStream; import java.io.IOException; import java.io.File; import java.util.Vector; import java.util.List; import java.util.ArrayList; import java.util.Iterator; import java.util.Enumeration; import java.util.Collections; import java.io.InputStream; import java.io.FileNotFoundException; /* javac -d classes "src/org/lgy/study/io/SequenceInputStreamTest2.java" java org.lgy.study.io.SequenceInputStreamTest2 */ public class SequenceInputStreamTest2{ public static void main(String[] args){ // method1(); // method2(); method3(); } // 通过Collection.enumeration(Collection<T> c)来获得Enumeration对象 public static void method3(){ List<InputStream> list = new ArrayList<>(); File rootFile = new File("./src/org/lgy/study/io"); FileInputStream fis1 = null, fis2 = null, fis3 = null; FileOutputStream fos = null; SequenceInputStream sqis = null; try{ fis1 = new FileInputStream(new File(rootFile, "1.txt")); fis2 = new FileInputStream(new File(rootFile, "2.txt")); fis3 = new FileInputStream(new File(rootFile, "3.txt")); fos = new FileOutputStream(new File(rootFile, "123.txt")); list.add(fis1); list.add(fis2); list.add(fis3); Enumeration<InputStream> en = Collections.enumeration(list); sqis = new SequenceInputStream(en); byte[] buf = new byte[512]; int size = -1; while((size = sqis.read(buf)) != -1){ fos.write(buf, 0, size); } }catch(FileNotFoundException e){ e.printStackTrace(); }catch(IOException e){ e.printStackTrace(); }finally{ if(sqis != null){ try{ sqis.close(); }catch(IOException e){ e.printStackTrace(); } } if(fos != null){ try{ fos.close(); }catch(IOException e){ e.printStackTrace(); } } } } // 使用匿名内部类来创建Enumeration public static void method2(){ List<InputStream> list = new ArrayList<>(); File rootFile = new File("./src/org/lgy/study/io"); FileInputStream fis1 = null, fis2 = null, fis3 = null; FileOutputStream fos = null; SequenceInputStream sqis = null; try{ fis1 = new FileInputStream(new File(rootFile, "1.txt")); fis2 = new FileInputStream(new File(rootFile, "2.txt")); fis3 = new FileInputStream(new File(rootFile, "3.txt")); fos = new FileOutputStream(new File(rootFile, "123.txt")); list.add(fis1); list.add(fis2); list.add(fis3); Iterator<InputStream> iterator = list.iterator(); Enumeration<InputStream> en = new Enumeration<InputStream>(){ @Override public boolean hasMoreElements(){ return iterator.hasNext(); } @Override public InputStream nextElement(){ return iterator.next(); } }; sqis = new SequenceInputStream(en); byte[] buf = new byte[512]; int size = -1; while((size = sqis.read(buf)) != -1){ fos.write(buf, 0, size); } }catch(FileNotFoundException e){ e.printStackTrace(); }catch(IOException e){ e.printStackTrace(); }finally{ if(sqis != null){ try{ sqis.close(); }catch(IOException e){ e.printStackTrace(); } } if(fos != null){ try{ fos.close(); }catch(IOException e){ e.printStackTrace(); } } } } // 使用Vector的elements方法得到Enumeration对象 public static void method1(){ File rootFile = new File("./src/org/lgy/study/io"); FileInputStream fis1 = null, fis2 = null, fis3 = null; FileOutputStream fos = null; SequenceInputStream sqis = null; try{ fis1 = new FileInputStream(new File(rootFile, "1.txt")); fis2 = new FileInputStream(new File(rootFile, "2.txt")); fis3 = new FileInputStream(new File(rootFile, "3.txt")); fos = new FileOutputStream(new File(rootFile, "123.txt")); Vector<InputStream> v = new Vector<>(); v.add(fis1); v.add(fis2); v.add(fis3); sqis = new SequenceInputStream(v.elements()); byte[] buf = new byte[512]; int size = 0; while((size = sqis.read(buf)) != -1){ fos.write(buf, 0, size); } }catch(FileNotFoundException e){ e.printStackTrace(); }catch(IOException e){ e.printStackTrace(); }finally{ if(sqis != null){ try{ sqis.close(); }catch(IOException e){ e.printStackTrace(); } } if(fos != null){ try{ fos.close(); }catch(IOException e){ e.printStackTrace(); } } } } }
----------------------
ASP.Net+Unity开发、.Net培训、期待与您交流! ----------------------
相关文章推荐
- 黑马程序员java学习,IO字节流及File
- 黑马程序员——Java IO字符流、字节流(小练习)
- 黑马程序员——Java基础---IO(一)---IO流概述、字符流、字节流、流操作规律
- 黑马程序员—java技术blog—第一篇 IO概述及字节流总结
- 黑马程序员——Java基础---IO(一)---IO流概述、字符流、字节流、流操作规律
- 黑马程序员——Java IO—字节流—PrintStream
- 黑马程序员——Java基础---IO(一)---IO流概述、字符流、字节流、流操作规律
- 黑马程序员——java基础——IO--字节流,字符流
- 『黑马程序员』---java--IO--字节流基础
- 黑马程序员——Java IO—字节流—DataInputStream 和 DataOutputStream
- 黑马程序员——Java IO—字节流—ObjectInputStream和ObjectOutputStream
- 黑马程序员——java的IO之字符流,字节流,转换流
- 黑马程序员---java基础之IO(字符流和字节流)
- 黑马程序员-java自学笔记02之IO字节流写入和读取
- 黑马程序员——Java IO—字节流—PipedInputStream和PipedOutputStream
- 黑马程序员——Java IO—字节流—BufferedInputStream和BufferedOutputStream
- 黑马程序员——Java基础----IO(字节流、操作规律等)(2)
- 黑马程序员——Java基础---IO(二)---IO字节流、流操作规律
- Java基础---IO(一)---IO流概述、字符流、字节流、流操作规律 (黑马程序员)
- 黑马程序员——Java基础---IO(一)---IO流概述、字符流、字节流、流操作规律