삼성SDS_멀티캠퍼스/Java

17일 차 입출력을 통해 파일복사

박성우기 2015. 10. 1. 10:40
반응형

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;


public class Test2 {


public static void main(String[] args) {

FileInputStream in = null;

FileOutputStream out = null;


try {


in = new FileInputStream("ocjp덤프.pdf");

out = new FileOutputStream("ocjp덤프_복사본.pdf");

//ocjp덤프.pdf의 모든 바이트를 읽어서 ocjp덤프_복사본.pdf에 그대로 써보기

byte[] buf = new byte[1024 * 4];

//read(byte[])를 통해 읽고 write(byte[])를 통해 쓴다.

//읽고 쓰는 바이트 배열의 크기는 4kb로 지정

int length;

while( (length = in.read(buf)) != -1)

//읽어들일 데이터가 없을때까지 계속 buf크기만큼씩 읽어들임

{

out.write(buf);

}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

try {

if (in != null)

in.close();

if (out != null)

out.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}


}

}



실제로 잘 읽힘



그런데 이렇게하면 용량에서 차이가 좀 난다.



그래서







빨간줄 친대로 해야 완벽히 복사

반응형