-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopyExample.java
More file actions
26 lines (22 loc) ยท 1015 Bytes
/
CopyExample.java
File metadata and controls
26 lines (22 loc) ยท 1015 Bytes
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
package ch18.sec03.exam03;
import java.io.*;
public class CopyExample {
public static void main(String[] args) throws IOException {
String originalFileName = "temp/bee.png";
String targetFileName = "temp/bee2.png";
// ์
์ถ๋ ฅ ์คํธ๋ฆผ ์์ฑ
InputStream is = new FileInputStream(originalFileName);
OutputStream os = new FileOutputStream(targetFileName);
// 8bit == 1byte
byte[] data = new byte[1024]; // ์ฝ์ ๋ฐ์ดํธ๋ฅผ ์ ์ฅํ ๋ฐฐ์ด ์์ฑ
while (true) {
int num = is.read(data); // ์ต๋ 1024 ๋ฐ์ดํธ๋ฅผ ์ฝ๊ณ ๋ฐฐ์ด์ ์ ์ฅํ๊ณ ์ฝ์ ๋ฐ์ดํธ๋ ๋ฐํ
if (num == -1) break; // ํ์ผ์ ๋ค ์ฝ์ผ๋ฉด while ๋ฌธ ์ข
๋ฃ
os.write(data, 0, num); // ์ฝ์ ๋ฐ์ดํธ ์๋งํผ ์ถ๋ ฅ
}
os.flush(); // ๋ด๋ถ ๋ฒํผ ์๋ฅ ๋ฐ์ดํธ๋ฅผ ์ถ๋ ฅํ๊ณ ๋ฒํผ ๋น์
os.close();
is.close();
System.out.println("๋ณต์ฌ๊ฐ ์ ๋์์ต๋๋ค.");
}
}