파일의 내용을 읽어서 출력하는 예제 코드와 비슷하다. 다만 화면으로 출력(System.out.write() = PrintStream.write())하지 않고 FileOutputStream으로 출력하는 부분만 다르다. write()메소드도 모두 OutputStream으로부터 확장된 write메소드를 사용하기 때문에 동일하다.

 

  

package ch4;

 

import java.io.*;

 

public class FileStreamCopy {

 

       public static void main(String[] args) {

              if(args.length != 2){

                     System.out.println("사용법 : java FileStreamCopy 파일1 파일2");

                     System.exit(0);

              } // if end

                          

              FileInputStream fis = null;

              FileOutputStream fos = null;

              try{

                     fis = new FileInputStream(args[0]);

                     fos = new FileOutputStream(args[1]);

                     byte[] buffer = new byte[512];

                     int readcount = 0;

                     while((readcount = fis.read(buffer)) != -1){

                           fos.write(buffer,0,readcount);

                     }

                     System.out.println("파일복사가 완료되었습니다.");

                    

              }catch(Exception ex){

                     System.out.println(ex);

              }finally{

                     try {

                           fis.close();

                     } catch (IOException ex) {}

                     try {

                           fos.close();

                     } catch (IOException ex) {}

              }

       } // main

}

 

 

 

 

 

 

 

Posted by Steven J.S Min
,