본문 바로가기

AWS CLOUD FRAMEWORK/Java

[Day17] Ex04

package file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class Ex04 {
	public static void main(String[] args) throws Exception {
		// 텍스트가 아닌, 바이너리 파일을 바이트 단위로 복사하는 예제
		
		File src = new File("11.jpg");
		File dst = new File("copy.jpg");
		
		System.out.println("src가 존재하는가 : " + src.exists());
		System.out.println("dst가 존재하는가 : " + dst.exists());
		
		FileInputStream fis = new FileInputStream(src);		// src의 내용을 읽어서
		FileOutputStream fos = new FileOutputStream(dst);	// dst에 기록한다
		
		int i = 0;
		int total = 0;
		
		// 1바이트씩 파일의 내용을 읽어서 새로 쓰는(복사하는) 코드 (젓가락질)
//		while((i = fis.read()) != -1) {
//			System.out.printf("%3d, %02X\n", i, i);
//			fos.write(i);
//			total++;
//		}
		
		// 1024바이트 크기의 그릇을 이용하여 그릇이 꽉 차면 옮겨담는 방식
		byte[] buf = new byte[1024];
		
		while((i = fis.read(buf)) != -1) {
			System.out.println(i);
			fos.write(buf);
			total += i;
		}
		
		System.out.println("total : " + total);
		System.out.println("dst 파일의 크기 : " + dst.length());
		
		fis.close();
		fos.close();
		
		
	}
}

'AWS CLOUD FRAMEWORK > Java' 카테고리의 다른 글

[Day17] Ex06  (0) 2023.04.06
[Day17] Ex05  (0) 2023.04.06
[Day17] Ex03  (0) 2023.04.06
[Day17] Ex02  (0) 2023.04.06
[Day17] Ex01  (0) 2023.04.06