IO(입출력) 개념1(InputStream)

·2022년 11월 5일
0

JAVA객체지향_IO

목록 보기
1/7

Input/Output: 데이터를 읽어오거나 출력하는 기능을 함
스트림(Stream): 데이터의 흐름

  • 데이터 처리 단위
    1바이트 스트림: InputStream, OutputStream
    2바이트 스트림: char 문자처리 => ~Reader, ~Writer
  • 주요 메소드
    read(): int형으로 읽어오는 메소드.
    ->byte단위로 읽어오기 때문에 숫자를 입력해도 해당 숫자로 읽어내지 않는다.(ASKII코드값으로 출력), IOException 예외 발생.
    ->더이상 읽어올게 없으면 -1을 리턴한다.
    write(): 출력 메소드. IOException 예외 발생.
    flush(): 버퍼가 차지 않아도 강제로 출력시키는 것
    close(): 닫아주는 메소드. IO는 명시적으로 닫아주는게 필요함

InputStream: 1바이트 단위로 읽어오는 스트림클래스

InputStream in = System.in;	//키보드로부터 입력
try{
	int n = in.read(); //System.in으로부터 1바이트씩 읽어오는 기능
    System.out.println(n);		//숫자 하나 출력
    System.out.println((char)n);//문자 하나 출력
}catch(IOException ie){
	System.out.println(ie.getMessage());
}

InputStreamReader: 2바이트 단위로 읽어오는 스트림클래스

  • 문법
    생성자-> InputStreamReader(InputStream in)
    InputStream in = System.in;(키보드로부터 입력받는다면..)
    InputStreamReader is = new InputStreamReader(in);
InputStreamReader is = new InputStreamReader(System.in);
try{
	int n = is.read();
    System.out.println(n);		//숫자 하나 출력
    System.out.println((char)n);//문자 하나 출력(2바이트라서 한글도 출력 ㄱㄴ)
}catch(IOException ie){
	System.out.println(ie.getMessage());
}
profile
웹개발입문자

0개의 댓글