본문 바로가기

Java

File I/O Ex. Source(2)

FileCopy2.java

/***

|

| Date : 2010-03-31

| Subject : File I/O Stream Test

| Contance :

|

*/

 

import java.io.*;

 

public class FileCopy2{

    public static void main(String[] args) {

        String readFile="angel.jpg";

        String writeFile="Copy_file.jpg";

 

        FileInputStream fis=null;

        FileOutputStream fos=null;

 

        //1. 연결

        try{

        //읽을 파일과 연결

        fis=new FileInputStream(readFile);    //객체 생성되면 읽을 파일과 연결 끝.

        //쓸 파일과 연결

        fos=new FileOutputStream(writeFile);    //객체 생성되면 쓸 파일과 연결 끝-파일이 없으면 새롭게 생성. 연결된 파일은 0byte 파일이 된다.(새로 쓰기);

        

 

        //3. I/O(읽고 /쓰기)

        //모든 파일의 끝(EOF, End Of File)에는 해당 파일의 끝임을 알리는 레코드가 있고 이 값은 -1이다.

            long l1=System.currentTimeMillis();

 

        byte [] buffer=new byte[2000];

        int readCount=fis.read(buffer);        //읽은 Byte 수가 readCount에 들어간다. & 최종적으로 EOF를 읽으면 -1이 들어간다.

 

        while(readCount!=-1){

            fos.write(buffer,0,readCount);    //0 : Start Index, readCount : length

            readCount=fis.read(buffer);

        }

            long l2=System.currentTimeMillis();

            System.out.println(l2-l1);     // 복사하는 데 소요된 시간을 측정, Performance 확인

 

        }catch (IOException ioe){

            System.out.println(ioe.getMessage());

        }

 

        finally{

        //4. 연결 끊기

            if(fis!=null){

                try{

                fis.close();

                }catch (IOException ioe){

                    System.out.println(ioe.getMessage());

                    }

            }

            if(fos!=null){

                try{

                    fos.close();

                }catch(IOException ioe){

                System.out.println(ioe.getMessage());

                }

            }

        }

 

    }

}

'Java' 카테고리의 다른 글

0401 Report Source  (0) 2010.04.01
PrintWriter Ex. Source  (0) 2010.04.01
PrintWriter Ex. Source  (0) 2010.04.01
Data In/OutPut StreamTest  (0) 2010.03.31
File IO using Buffered IO Stream  (0) 2010.03.31
File I/O Ex. Source(1)  (0) 2010.03.31
School_Collection_Ex  (0) 2010.03.30
HashMap Ex. Source(2)  (0) 2010.03.30
HashMap Ex. Source  (0) 2010.03.30
Collection FrameWork 中 MAP Ex. source  (0) 2010.03.30