명품 JAVA 프로그래밍

명품 JAVA 프로그래밍 8장 실습문제 7

anycoding 2021. 12. 8. 22:20
반응형

문제

파일 복사 연습을 해보자. 이미지 복사가 진행하는 동안 10% 진행할 때마다 '*' 하나씩 출력하도록 하라. 아래 예시는 프로젝트 폴더 밑에 a.jpg을 미리 준비해두었다.

 

출력 예시

a.jpg를 b.jpg로 복사합니다.
10%마다 *를 출력합니다.
**********

 

소스 코드

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

public class Main7 {

	public static void main(String[] args) {
		File src = new File("파일경로\\a.jfif");
		File dest = new File("파일경로\\b.jfif");
		int c;
		int count = 0,div = 1;
		try {
			FileInputStream fi = new FileInputStream(src);
			FileOutputStream fo = new FileOutputStream(dest);
			System.out.println(src.getPath() + "를 "+ dest.getPath()+"로 복사합니다");
			System.out.println("10%마다 *를 출력합니다");
			while((c=fi.read())!=-1) {
				fo.write((byte)c);
				count++;
				if(((src.length())/10*div ) == count ) {
					System.out.print("*");
					div++;
				}
			}
			fi.close();
			fo.close();
			
		}catch(IOException e) {
			System.out.println("파일 복사 오류");
		}
	}

}

jpg가 아닌 jfif그림파일을 사용했습니다.

 

a.jfif
0.02MB
b.jfif
0.02MB

반응형