ByteArrayOutputStream을 이용한 파일복사 문제
그림자
안녕하세요?
I/O 공부중에 ByteArrayOutputStream를 이용한 파일복사 방법있어 하던중 이상하게 복사된 파일의 크기가 지나치게 원본파일보다 크네요
FileOutputStream으로는 잘 되는데...
아래 소스입니다.
제가 노친 부분이 있거나 이해를 잘 못하는 부분이 있으면 설명 부탁을 드려봅니다.
감사합니다.import java.io.*;
public class ByteStreamText {
public static void main(String[] ar){
FileInputStream fis;
FileOutputStream fos;
ByteArrayOutputStream baos;
File dir = new File(D:\\Java\\InputOutput\\Chapter06\\files);
File orignal = new File(dir, aaa.jpg);
File copy = new File(dir, ddd.jpg);
byte[] buffer = new byte[512];
int readcount;
try{
fis = new FileInputStream(orignal);
fos = new FileOutputStream(copy);
baos = new ByteArrayOutputStream();
while((readcount = fis.read(buffer)) != -1) {
// fos.write(buffer, 0, readcount); -- FileOutputStream으로 할때는 잘 됩니다...
baos.write(buffer, 0, readcount);
baos.writeTo(fos); -- ByteArrayOutputStream의 writeTo(OutputStream) 메소드를 이용한 파일 복사
}
baos.flush();
baos.close();
fis.close();
fos.close();
}catch(FileNotFoundException ee){
ee.printStackTrace();
}catch(IOException ee){
ee.printStackTrace();
}
}
}
-
달
친절한 답변 감사드립니다.
담긴것을 한꺼번에 FileOutputStream에 넣어야 하는군요.. -
이플
순서를 보시면..
byte buffer에 읽은 크기[read size :512]만큼 저장[total buffer size: 512]. file write [512]
읽은 데이터[read size :512]를 추가[total buffer size: 1024] - file write [512+1024]
.......
원하시는 형식은 아닐거 같네용..
1. while 문 안에서 ByteArrayOutputStream의 객체를 생성 - file wr