-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSequenceInputStreamDemo.java
More file actions
65 lines (60 loc) · 2.26 KB
/
Copy pathSequenceInputStreamDemo.java
File metadata and controls
65 lines (60 loc) · 2.26 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package io;
import java.io.*;
import java.util.Enumeration;
import java.util.Vector;
/**
* Created by helmeter on 5/10/16.
*/
public class SequenceInputStreamDemo {
/**
* @param args
* SequenceInputStream合并流,将与之相连接的流集组合成一个输入流并从第一个输入流开始读取,
* 直到到达文件末尾,接着从第二个输入流读取,依次类推,直到到达包含的最后一个输入流的文件末尾为止。
* 合并流的作用是将多个源合并合一个源。可接收枚举类所封闭的多个字节流对象。
*/
public static void main(String[] args) {
doSequence();
}
private static void doSequence() {
// 创建一个合并流的对象
SequenceInputStream sis = null;
// 创建输出流。
BufferedOutputStream bos = null;
BufferedInputStream bufferedInputStream = null;
try {
// 构建流集合。
Vector<InputStream> vector = new Vector<InputStream>();
vector.addElement(new FileInputStream("img/student1.txt"));
vector.addElement(new FileInputStream("img/student2.txt"));
vector.addElement(new FileInputStream("img/student3.txt"));
Enumeration<InputStream> e = vector.elements();
sis = new SequenceInputStream(e);
bufferedInputStream = new BufferedInputStream(sis);
bos = new BufferedOutputStream(new FileOutputStream("img/student.txt"));
// 读写数据
byte[] buf = new byte[1024];
int len = 0;
while((len = bufferedInputStream.read(buf)) != -1) {
bos.write(buf, 0, len);
bos.flush();
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} finally {
try {
if (sis != null)
sis.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (bos != null)
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}