본문 바로가기
자바웹프로그래밍/JAVA

Reader,Writer(문자 기반 스트림) with java

by 디찌s 2024. 2. 27.
728x90
반응형

 

 

 

 

문자 기반 스트림

이전 글에서 바이트 기반 스트림을 알아보았다. 이번에는 문자기반 스트림에대해 알아볼것이다.

2024.02.27 - [자바웹프로그래밍/JAVA] - InputStream,OutputStream 입출력 방식 분석 with java

 

 

Reader와 Writer 란?

Reader 는 문자 기반 입력 스트림 최상위 추상 클래스이고, Writer 는 문자 기반 출력 스트림 최상위 추상 클래스이다. 이들의 하위 클래스는 XXXReader , XXXWriter 이라는 오버라이드된

 

 

Reader 소개

 

 

read() 메소드

 public int read() throws IOException {
        char[] cb = new char[1];
        if (read(cb, 0, 1) == -1)
            return -1;
        else
            return cb[0];
    }

 

Reader의 read 메소드는 한번 읽을떄 2바이트를 읽고 int형(4바이트)로 돌려준다. 

 

Writer 소개

 

 

wirte() 메소드

 

/**
     * Writes a single character.  The character to be written is contained in
     * the 16 low-order bits of the given integer value; the 16 high-order bits
     * are ignored.
     *
     * <p> Subclasses that intend to support efficient single-character output
     * should override this method.
     *
     * @param  c
     *         int specifying a character to be written
     *
     * @throws  IOException
     *          If an I/O error occurs
     */
    public void write(int c) throws IOException {
        synchronized (lock) {
            if (writeBuffer == null){
                writeBuffer = new char[WRITE_BUFFER_SIZE];
            }
            writeBuffer[0] = (char) c;
            write(writeBuffer, 0, 1);
        }
    }

 

Writer 의 write는 반대로 2바이트(char)씩 읽는다.

 

 

728x90
반응형

댓글