package xyz.itwill.io;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
//파일에 저장된 값을 문자데이타를 읽어와 모니터에 전달하여 출력하는 프로그램
public class FileCharLoadApp {
public static void main(String[] args) throws IOException {
//FileReader : 파일에 저장된 값을 문자데이타로 읽기 위한 입력스트림을 생성하기 위한 클래스
FileReader in=null;
try {
//FileReader(String filename) : 파일경로를 전달받아 파일 입력스트림을
생성하는 생성자
// => 전달받은 파일경로에 파일이 없는 경우 FileNotFoundException 발생 - 예외처리
// => 파일이 없는 경우 파일 입력스트림을 생성하지 못하므로 반드시 예외처리
in=new FileReader("c:/data/char.txt");
//문자데이타를 전달하기 위한 모니터 출력스트림으로 확장
OutputStreamWriter out=new OutputStreamWriter(System.out);
System.out.println("[메세지]c:\\data\\char.txt 파일에 저장된 내용입니다.");
int readByte;
while(true) {
//파일 입력스트림을 이용하여 파일값을 문자데이타로 반환받아 저장 - LOAD
readByte=in.read();
if(readByte==-1) break;//파일의 모든 값을 얻어온 경우 반복문 종료
//모니터 출력스트림에 문자데이타를 전달하여 출력
out.write(readByte);
out.flush();
}
} catch (FileNotFoundException e) {
System.out.println("[에러]대상 파일을 찾을 수 없습니다.");
} finally {
//FileReader.close() : 파일 입력스트림을 제거하는 메소드
if(in!=null) in.close();
}
}
}