package kr.or.didt.basic;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class SemCopy {
public static void main(String[] args) {
String fileName = "펭귄.jpg";
File file = new File("d:/d_other/" + fileName);
if(!file.exists()){
System.out.println(file.getPath() + "파일이 없습니다.");
System.out.println("복사 작업을 중단합니다.");
return;
}
String targetFileName = "복사본_펭귄.jpg";
File targetFile = new File("d:/d_other/연습용");//연습용 폴더에 복사본이 들어가는 것
if(!targetFile.exists()){ //'연습용' 폴더가 없으면...
targetFile.mkdir(); //'연습용' 폴더를 만든다.
}
try {
//복사할 원본 파일에 사용할 스트림 객체 생성
FileInputStream fin = new FileInputStream(file);
//그림파일은 문자 데이터가 아니기 때문에 바이트 기반의 스트림을 사용하는 것
//복사될 대상 파일에 사용할 스트림 객체 생성
FileOutputStream fout =
new FileOutputStream(targetFile.getPath() //경로를 나오게하는ㄴ것
+ File.separator + targetFileName);
//경로와 파일 이름 사이에 슬레시나 역슬레시를 시스템에 맞게 자동으로 넣어줌
System.out.println("복사 시작...");
int data; //읽어온 데이터가 저장될 변수
while((data = fin.read()) != -1){
fout.write(data);
}
//스트림 닫기
fin.close();
fout.close();
System.out.println("복사 완료...");
} catch (IOException e) {
// TODO: handle exception
}
}
}