您的位置:首页 > 产品设计 > UI/UE

黑马程序员——Java IO—字节流—SequenceInputStream

2014-10-04 07:03 429 查看
----------------------
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培训、期待与您交流! ----------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐