forked from JustinSDK/JavaSE6Tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBufferedStreamDemo.java
More file actions
45 lines (36 loc) · 1.35 KB
/
BufferedStreamDemo.java
File metadata and controls
45 lines (36 loc) · 1.35 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
package onlyfun.caterpillar;
import java.io.*;
public class BufferedStreamDemo {
public static void main(String[] args) {
try {
byte[] data = new byte[1];
File srcFile = new File(args[0]);
File desFile = new File(args[1]);
BufferedInputStream bufferedInputStream =
new BufferedInputStream(
new FileInputStream(srcFile));
BufferedOutputStream bufferedOutputStream =
new BufferedOutputStream(
new FileOutputStream(desFile));
System.out.println("複製檔案:" +
srcFile.length() + "位元組");
while(bufferedInputStream.read(data) != -1) {
bufferedOutputStream.write(data);
}
// 將緩衝區中的資料全部寫出
bufferedOutputStream.flush();
// 關閉串流
bufferedInputStream.close();
bufferedOutputStream.close();
System.out.println("複製完成");
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println(
"using: java UseFileStream src des");
e.printStackTrace();
}
catch(IOException e) {
e.printStackTrace();
}
}
}