들어가기에 앞서, PrintWriter 는 Reader가 없는 오직 출력을 위한 객체입니다.

사용 빈도가 다른 Reader들에 비해 높다고 하니, 꼭 참고 하시기 바랍니다!

아래 예제는 쓰는 방법에 대해서만 설명하였으니, 응용하는 방법은 뒤에서 더 자세히 다루겠습니다.

 

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class Ex01PrintWriter {

 public static void main(String[] args) throws IOException {
  
  //PrintWriter writer = new PrintWriter(System.out,true); // Auto flush 기능
  
  PrintWriter writer = new PrintWriter(new FileWriter("/home/pc36/io/print.txt"),true); // Auto flush 기능 및 파일이 출력될 경로 지정
  
  writer.println(">>> 개인 정보 출력 <<<");
  
  writer.printf("%s, %d, %c, %s\n", "이름1", 25, 'M', "010-1234-5678");
  writer.printf("%s, %d, %c, %s\n", "이름2", 25, 'F', "010-4567-8910");
  writer.printf("%s, %d, %c, %s\n", "이름3", 35, 'M', "010-1845-9541");
  
  // writer.flush(); => Auto flush 가 true이므로 따로 입력해주지 않아도 됩니다.
  
  System.out.println("데이터 출력 완료.");  
 }
}

+ Recent posts