System.out.println("Hello, World");

import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class FileStreamArrayTest { public static void main(String[] args) { FileInputStream fi = null; FileOutputStream fo = null; try { //1. 연결 fi = new FileInputStream("D:\\Desert.jpg");//연결 fo = new FileOutputStream("D:\\Desert3.jpg");//덮어쓰기 //fo=new FileOutputStream("D:\\Desert2.jpg",true);//이어쓰기 파일크기가 2배가 되어버린다. //2. I/O작업(읽는 작업, 출력작업) long l1 = System.currentTimeMillis(); int count =0; byte[] buff = new byte[10000];//읽은 데이터는 배열에 있다. int length = fi.read(buff);//몇바이트 읽었는지 while(length != -1){ fo.write(buff,0,length);// 0번부터 length까지 쓰게 된다. 그냥 buff로 쓰면 기존에 있던 바이트들이 중복처리되서 파일 용량이 이전것보다 크게 써지게 된다.

length =fi.read(buff);// count++; } long l2 = System.currentTimeMillis(); System.out.println(l2-l1+"초"); System.out.println("반복횟수 : "+count); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ if(fi!=null){//만약 fi가 null이 아닐 경우 try { fi.close(); } catch (IOException e) { e.printStackTrace(); } } if(fo!=null){ try { fo.close(); } catch (IOException e) { e.printStackTrace(); } } } } }


Posted by 김마농