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

chapter02 : FileInput 본문

JAVA/chapter24_io

chapter02 : FileInput

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

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class Ex02_FileInput {
	public static void main(String[] args) {
      
		FileInputStream fis = null;
		BufferedInputStream bis = null;
		
		try {
			fis = new FileInputStream("alphabet.txt");
			bis = new BufferedInputStream(fis);
			int ch = 0;         //char ch;가 아님을 주의! 읽을 때에는 int
			while(true) {
				ch = bis.read();//read()의 리턴이 int 타입
				if(ch == -1) {   //read()의 파일이 끝나면 -1을 리턴
					break;
				}
				System.out.print((char)ch);
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				if(bis != null) {bis.close();}
				if(bis != null) {fis.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
chapter03 : FileOutput  (0) 2023.05.31
chapter01 : FileOutput  (0) 2023.05.31