본문 바로가기

Java

File IO using Buffered IO Stream

FileCopyBuffered.java

/***

|

| Date : 2010-03-31

| Subject : File I/O Stream Test using Buffer Filter

| Contance :

|

*/

 

import java.io.*;

 

public class FileCopyBuffered{

    public static void main(String[] args) {

        String readFile="angel.jpg";

        String writeFile="Copy_file.jpg";

 

        FileInputStream fis=null;

        FileOutputStream fos=null;

 

        //Buffer를 이용해 입출력 하는 Filter 계열의 Stream

        BufferedInputStream bis=null;;

        BufferedOutputStream bos=null;

 

        

        //1. 연결

        try{

    

        fis=new FileInputStream(readFile);    

        fos=new FileOutputStream(writeFile);    

        

        //2. Filter-- FilterStream 객체를생성하면서 기존 Stream 객체를 Argument로 넘긴다.

        bis=new BufferedInputStream(fis);

        bos=new BufferedOutputStream(fos);

 

        //3. I/O

        

        long l1=System.currentTimeMillis();

 

        int readData=bis.read();

 

        while(readData!=-1){

            bos.write(readData);

            bos.flush();    

//일반적으로 Buffer가 Full 이어야만 Data를 전송하지만, flush()를 통해 Buffer가 Not Full 상황에서도 전송하도록 설정.

//이 source에서는 While문안에서 flush()를 수행함으로써 , 루프를 돌 때마다 강제적으로 전송을 수행하였다. 이는 매우 Perfomance적으로 좋지 않은 방법이다.

            readData=bis.read();

        }

            //bos.flush();

            long l2=System.currentTimeMillis();

            System.out.println(l2-l1);

 

        }catch (IOException ioe){

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

        }

 

        finally{

        //4. 연결 끊기

            if(bis!=null){

                try{

                bis.close();

                }catch (IOException ioe){

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

                    }

            }

            if(bos!=null){

                try{

                    bos.close();

                }catch(IOException ioe){

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

                }

            }

        }

    }

}

'Java' 카테고리의 다른 글

Tomcat 설치 및 간단한 환경설정  (0) 2010.04.22
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 I/O Ex. Source(2)  (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