자바 파일 입출력2

JAVA 2012. 3. 30. 01:18

 

출처 : http://joongang.springnote.com/pages/4642959.xhtml

 

  1. import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
  2. public class FileIOEx5 {
  3.  public static void main(String[] args)
     {
      FileReader fr = null;   // 파일로 부터 텍스트 데이터를 읽는데 사용함.
      int c1;
      int c2;
      int c3;
      try {
       fr = new FileReader("c:\\test.txt");
       
    //   c1 = fr.read();
    //   c2 = fr.read();
    //   c3 = fr.read();
    //   
    //   System.out.println((char)c1);
    //   System.out.println((char)c2);
    //   System.out.println((char)c3);
       int c =0;
       while((c = fr.read()) != -1)
       {
        System.out.print((char)c); // 한글자씩 읽어서 출력하고 끝까지 읽음.(-1)
  4.                                // test.txt파일에서 하나씩 출력하고 콘솔에 나타내고 끝까지 읽음.
       }
      }
      catch (FileNotFoundException e)
      {
       e.printStackTrace();
      }
      catch (IOException e)
      {
       e.printStackTrace();
      }
      finally
      {
       if(fr != null) try{ fr.close(); }  catch(IOException e) {}
      }
     }
    }

'JAVA' 카테고리의 다른 글

[JAVA] static키워드 바로알기  (0) 2014.01.09
자바 무료강의 링크 사이트  (0) 2013.12.30
자바 api  (0) 2013.03.14
이클립스 자바 폰트 설정  (0) 2012.05.28
자바 파일 입출력 1  (0) 2012.03.30