-> Node Stream 을 보조하는 역할이다.
Node Stream Ex) FileInputStream, FileOutputStream
-> FileNotFoundException : 경로 파일이 없을 경우에 대한 예외
-> IOException : 생성 시 파일 경로 자체를 잘못 적었을 경우
File file_2 = new File("ㅋ"); // 절대경로를 잘못 적은 경우
try{
String src_fileName = "파일의 절대경로";
File src_file = new File(src_fileName);
FileInputStream fist = new FileInputStream(src_file);
// 보조(필터)스트림 장착
BufferedInputStream bist
= new BufferedInputStream(fist, 1024*1024); // 1 mb
// new BufferedInputStream(fist) 하는 경우
// 기본값 : 0.5 kb = 512byte 가 된다.
String target_fileName = "파일의 절대경로(복사할 위치)";
File target_file = new File(target_fileName);
FileOutputStream fost = new FileOutputStream(target_file);
// 보조(필터)스트림 장착
BufferedOutputStream bost
= new BufferedOutputStream(fost, 1024*1024*2); // 2mb
// new BufferedOutputStreamm(fost) 하는 경우
// 기본값 : 0.5 kb = 512byte 가 된다.
byte[] data_arr = new byte[1024*1024]; // 1mb 씩 배열
int input_length = 0; // 한번에 읽는 byte 수
int totalByte = 0; // byte 수 누적 용도
int cnt = 0; // 반복횟수 확인 용도
long src_file_size = src_file.length(); // 원본 파일의 크기
long max_size = 1024*1024*10; // 최대 크기 10mb
// == 파일 크기 제한두기 == //
if(src_file_size > max_size) { // 크기가 10mb 초과인 경우
System.out.println("파일이 10mb 이상으로 복사 불가능";
// 닫을 때는 보조(필터) 스트림 먼저 닫고, 노드 스트림 닫기
bist.close();
fist.close();
bost.close();
fost.close();
return; // 메소드 종료
}
while((input_length = bist.read(data_arr)) != -1) {
// bist 는 필터가 씌워진 fist 이다.
bost.write(data_arr, 0, input_length); // 쓰기
bost.flush(); // 내뿜기
totalByte += input_length; // 총 byte 수 누적
if(input_length == 1024*1024)
cnt++; // 반복횟수
} // end of while----------------------------
// 닫을 때는 보조(필터) 스트림 먼저 닫고, 노드 스트림 닫기
bist.close();
fist.close();
bost.close();
fost.close();
System.out.println("총 " + totalByte + "byte");
System.out.println("반복횟수 " + cnt + "번 반복함");
} catch(FileNotFoundException e) { // 파일이 없을 경우
} catch(IOException e) { } // end of try~catch----------
my.day20.c.io -> Stream구분, FileCopy_filterstream_main