Java.io之SequenceInputStream

SequenceInputStream是合并流,将多个InputStream数据依次合并成一个流。

SequenceInputStream的用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package cn.deepblog;

import java.io.*;
import java.util.*;


public class IOSourceCode8
{
public static void main(String[] args)throws IOException
{

Vector<FileInputStream> v = new Vector<FileInputStream>();

v.add(new FileInputStream("/Users/deep/Desktop/file1.txt"));
v.add(new FileInputStream("/Users/deep/Desktop/file2.txt"));
v.add(new FileInputStream("/Users/deep/Desktop/file3.txt"));

Enumeration<FileInputStream> en = v.elements();

SequenceInputStream sis = new SequenceInputStream(en);

FileOutputStream fos = new FileOutputStream("/Users/deep/Desktop/file4.txt");
byte[] buf = new byte[1024];
int len = 0;
while((len=sis.read(buf))!=-1)
{
fos.write(buf,0,len);
}
fos.close();
sis.close();
}
}
运行前:
file1.txt: 123
file1.txt: 456
file1.txt: 789

运行后:
file1.txt: 123456789
Donate here.