Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

WON.dev

chapter03 : FileOutput 본문

JAVA/chapter24_io

chapter03 : FileOutput

GAWON 2023. 5. 31. 09:20
package org.joonzis.ex;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class Ex03_FileOutput {
   public static void main(String[] args) {
      
      String msg = "가나다라마바사아자차카타파하"; // Scanner를 이용하여 받은 내용 저장 가능
      
      // 문자 기반 스트림 (파일 작성 시 주로 사용)
      File file = null;
      FileWriter fw = null;      // OutputStreamWriter 상속
      BufferedWriter bw = null;    // Writer 클래스 상속
      // 저용량의 파일 시 FileWriter만 사용해도 무방
      
      try {
         file = new File("hangeul.txt");
         fw = new FileWriter(file, false);            
         // true : 이어쓰기, false : 덮어쓰기 (옵션 미선택 시 기본 값은 false)
         bw = new BufferedWriter(fw);
         bw.write(msg);
         bw.flush();
         System.out.println("hangeul.txt 파일을 생성했습니다.");
      } catch (IOException e) {
         e.printStackTrace();
      } finally {
         try {
            if(bw != null) {bw.close();}
            if(fw != null) {fw.close();}
         } catch (Exception e2) {
            e2.printStackTrace();
         }
      }
   }
}

'JAVA > chapter24_io' 카테고리의 다른 글

chapter06 : FileCopy  (0) 2023.05.31
chapter05 : FileCopy  (0) 2023.05.31
chapter04 : FileInput  (0) 2023.05.31
chapter02 : FileInput  (0) 2023.05.31
chapter01 : FileOutput  (0) 2023.05.31