FileIOMain02

"Java Stuty"

Posted by Chungman on March 26, 2021

FileIO 예제

FileIOMain02

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
package Java0326.Lecture15.FileIO;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/*
 * IOdata/dog.jpg => IOdata/dog2.jpg 복사
 * 기계입장 :입력장치				출력장치
 * 
 * 	<작업순서>
 * 1. Stream open
 * 2. read/write
 * 3. Stream close
 */

public class FileIOMain02 {

	public static void main(String[] args) {

		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			
			long start = System.currentTimeMillis();
			
		// 1. Stream open	
			fis = new FileInputStream("Iodata/dog.jpg");
			fos = new FileOutputStream("Iodata/dog2.jpg");
		//	FileInputStream fis = new FileInputStream("Iodata/dog.jpg");
		//	FileOutputStream fos = new FileOutputStream("Iodata/dog2.jpg");
			
		// 2. read/write
			while(true) {
				int c = fis.read();
				if(c == -1) break;
				fos.write(c);
			}
			
			long end = System.currentTimeMillis();
			System.out.println("복사완료!");
//			while(true);					// 외부에서 lock이 걸려있기때문에 파일이 0바이트로 나옴
			System.out.println("소요시간 : " + (end - start)/1000. + "초");
			
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
		// 3. Stream close
			try {
				fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}

FileIOMain03

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package Java0326.Lecture15.FileIO;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class FileIOMain03 {

	// dog.jgp => dog3.jpg 복사
	public static void main(String[] args) {
	
		FileInputStream fis = null;
		BufferedInputStream bis = null;
		FileOutputStream fos = null;
		BufferedOutputStream bos = null;
		
		try {
			long start = System.currentTimeMillis();
			
			fis = new FileInputStream("Iodata/dog.jpg");
			fos = new FileOutputStream("Iodata/dog.jpg");
			
			// 버퍼사용 객체연결
			bis = new BufferedInputStream(fis);
			bos = new BufferedOutputStream(fos);
			
			// 복사수행
			while(true) {
				int c = bis.read();
				if(c == -1)break;
				bos.write(c);
			}
			
			long end = System.currentTimeMillis();
			System.out.println("복사완료!");
			System.out.println("소요시간 : " + (end - start)/1000. + "초");
			
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			
			FileClose.close(bis, fis);
			FileClose.close(bos, fos);
			/*
			FileClose.close(bis);
			FileClose.close(bos);
			FileClose.close(fis);
			FileClose.close(fos);
			*/
			/*
			if(bis != null) {
				try {
					bis.close();
				} catch(Exception e) {
					e.printStackTrace();
				}
			}
			if(bos != null) {
				try {
					bos.close();
				} catch(Exception e) {
					e.printStackTrace();
				}
			}
			if(fis != null) {
				try {
					fis.close();
				} catch(Exception e) {
					e.printStackTrace();
				}
			}
			if(fos != null) {
				try {
					fos.close();
				} catch(Exception e) {
					e.printStackTrace();
				}
			}
		*/
		}
		
	}

}