본문 바로가기

Java

PrintWriter Ex. Source

/***

|

| Date : 2010-04-01

| Subject : Console 창에서 KeyBoard로 입력한 내용물을 txt 파일로 저장하기

| Contance :

|

*/

 

 

import java.io.*;

public class ConsoleToFile{

    public static void main(String[] args)throws IOException{        

 

String target="target.txt";

    

    InputStream is=System.in;    //키보드와 연결

    InputStreamReader ir =null; //input stream --> Reader로 변환    

    BufferedReader br=null;

 

    FileWriter fw=null;

    PrintWriter pw=null;

 

    try{

    

        ir=new InputStreamReader(is);

        br=new BufferedReader(ir);

 

        fw=new FileWriter(target);

        pw=new PrintWriter(fw);

 

        System.out.println("종료하시려면 ctrl + z or ctrl+c를 누르세요");

        String str=br.readLine();        //InputStreamReader에는 ReadLine() Method가 없다.

                while(str!=null){

                System.out.println(str);

                pw.println(str);

                str=br.readLine();

                }

 

    }catch(IOException ioe){

        System.out.println(ioe);

 

    }finally{

 

            if(br!=null){

            try{

                br.close();

            }catch(IOException ioe){}

        }

    

        if(pw!=null){

                pw.flush();    //buffer Initializing

                pw.close();

        }

 

    }

 

}

}

'Java' 카테고리의 다른 글

Spring에서 자주 사용되어지는 API 묶음  (0) 2010.06.13
간단한 Spring Project 실습하기  (0) 2010.06.13
Spring 설치 및 이클립스와의 연동  (1) 2010.06.13
Tomcat 설치 및 간단한 환경설정  (0) 2010.04.22
0401 Report 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(2)  (0) 2010.03.31
File I/O Ex. Source(1)  (0) 2010.03.31