JAVA/(IO)Input_Output
이미지 복사하기
hyeonju50
2023. 8. 6. 23:46
package ex03_image_copy;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainWrapper {
public static void main(String[] args) {
//원본파일 객체 생성
File dir1 =new File("C:\\Users\\사용자명\\Pictures");
File src=new File(dir1,"Java_Logo.png");
//원본 읽는 입력 스트림 선언
BufferedInputStream bin =null;
//복사본 파일 객체 생성
File dir2=new File("C:/storage");
if(dir2.exists()==false)
{
dir2.mkdirs();
}
File cp =new File(dir2,src.getName());
//복사본 생성 출력 스트림 선언
BufferedOutputStream bout= null;
try {
//원본 읽는 입력스트림 생성
bin= new BufferedInputStream(new FileInputStream(src));
//복사본 출력 스트림
bout=new BufferedOutputStream(new FileOutputStream(cp));
//몇씩 복사
byte[]b=new byte[1024];
//원본에서 읽은 실제 바이트 수
int readByte=0;
//1024 복사
while((readByte=bin.read(b))!=-1)
{
bout.write(b,0,readByte);
}
System.out.println(src.getPath() + " → " + cp.getPath());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(bout != null) bout.close();
if(bin != null) bin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}