File I/O Ex. Source(1)
FileCopy.java |
/*** | | Date : 2010-03-31 | Subject : File I/O Stream Test | Contance : | */
import java.io.*;
public class FileCopy{ 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 파일이 된다.(새로 쓰기);
//2. Filtering(생략) //--------------------------------------------- //3. I/O //모든 파일의 끝(EOF, End Of File)에는 해당 파일의 끝임을 알리는 레코드가 있고 이 값은 -1이다. int readData=fis.read(); //int i=0; while(readData!=-1){ fos.write(readData); readData=fis.read(); }
}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()); } } } //--------------------------------------------- } } |