자바 7 의 전통 I / O - 문자 클래스 CharArray Reader 와 CharArray Writer
CharArray Reader 와 CharArray Writer 는 문자 배열 흐름 입 니 다.이것 은 ByteArray InputStream, ByteArray OutputStream 과 유사 합 니 다. ByteArray XXputStream 은 바이트 배열 흐름 이 고 CharArray XX 는 문자 배열 흐름 입 니 다.
CharArray Writer 는 문자 배열 을 읽 는 데 사 용 됩 니 다. Writer 클래스 에 계승 합 니 다.문자 단위 로 배열 작업 을 진행 합 니 다.다음은 주요 방법 으로 이 루어 진 소스 코드 를 살 펴 보 겠 습 니 다.
1. 생 성 및 초기 화
CharArray Writerer 에서 정의 하 는 중요 한 변수 와 구조 함 수 를 보십시오.
view source print ?
01.  protected   char   buf[];    //     ,          02.  protected   int   count;     //            03.  public   CharArrayWriter() {  04.        this  (  32  );  05.  }  06.  public   CharArrayWriter(  int    initialSize) {  07.        if   (initialSize <   0  ) {  08.            throw   new   IllegalArgumentException(  "Negative initial size: "   + initialSize);  09.        }  10.        buf =   new   char  [initialSize];  11.  }  기본 buf 배열 의 크기 는 32 이 며, 스스로 지정 할 수 있 습 니 다.
CharArray Reader 에서 정의 하 는 중요 한 변수 와 구조 함수:
view source print ?
01.  protected   char   buf[];           //     ,         02.  protected   int   pos;              //          03.  protected   int   markedPos =   0  ;    //         04.  05.  protected   int   count;            //            06.  07.  public   CharArrayReader(  char    buf[]) {  08.        this  .buf = buf;  09.        this  .pos =   0  ;  10.        this  .count = buf.length;  11.  }  12.  13.  public   CharArrayReader(  char    buf[],   int   offset,    int   length) {  14.        if   ((offset <   0  ) || (offset > buf.length) || (length <   0  ) || ((offset + length) <    0  )) {  15.            throw   new   IllegalArgumentException();  16.        }  17.        this  .buf = buf;  18.        this  .pos = offset;  19.        this  .count = Math.min(offset + length, buf.length);  20.        this  .markedPos = offset;  21.  }  2, CharArray Writerer 기록 데이터
view source print ?
01.  public   void   write(  int   c) {  02.        synchronized   (lock) {  03.            int   newcount = count +   1  ;  04.            if   (newcount > buf.length) {  05.                buf = Arrays.copyOf(buf, Math.max(buf.length <<   1  , newcount));  06.            }  07.            buf[count] = (  char  )c;  08.            count = newcount;  09.        }  10.  }  11.  12.  public   void   write(  char   c[],   int    off,   int   len) {  13.        if   ((off <   0  ) || (off > c.length) || (len <   0  ) || ((off + len) > c.length) || ((off + len) <   0  )) {  14.            throw   new   IndexOutOfBoundsException();  15.        }   else   if    (len ==   0  ) {  16.            return  ;  17.        }  18.        synchronized   (lock) {  19.            int   newcount = count + len;  20.            if   (newcount > buf.length) {  21.                buf = Arrays.copyOf(buf, Math.max(buf.length <<   1  , newcount));  22.            }  23.            System.arraycopy(c, off, buf, count, len);  24.            count = newcount;  25.        }  26.  }  27.  28.  public   void   write(String str,   int   off,   int    len) {  29.        synchronized   (lock) {  30.            int   newcount = count + len;  31.            if   (newcount > buf.length) {  32.                buf = Arrays.copyOf(buf, Math.max(buf.length <<   1  , newcount));  33.            }  34.            str.getChars(off, off + len, buf, count);  35.            count = newcount;  36.        }  37.  }  Write 클래스 에서 정의 하 는 write () 방법 과 append () 방법 을 실현 하 였 습 니 다.사실 현대 코드 는 비교적 간단 하 다. 앞에서 비슷 한 방법 은 이미 여러 번 말 했 으 니 여 기 는 더 이상 군말 하지 않 는 다.
3, CharArrayReader 읽 기 데이터
CharArray Reader 는 문자 배열 을 읽 는 데 사 용 됩 니 다. Reader 에 계승 합 니 다.조작 한 데 이 터 는 문자 단위 이다.다음은 주요 방법 으로 이 루어 진 소스 코드 를 살 펴 보 겠 습 니 다.
view source print ?
01.  public   int   read()   throws   IOException {  02.        synchronized   (lock) {  03.            ensureOpen();  04.            if   (pos >= count)  05.                return   -  1  ;  06.            else  07.                return   buf[pos++];  08.        }  09.  }  10.  11.  public   int   read(  char   b[],   int    off,   int   len)   throws   IOException {  12.        synchronized   (lock) {  13.            ensureOpen();  14.            if   ((off <   0  ) || (off > b.length) || (len <   0  ) ||  15.                ((off + len) > b.length) || ((off + len) <   0  )) {  16.                throw   new   IndexOutOfBoundsException();  17.            }   else   if    (len ==   0  ) {  18.                return   0  ;  19.            }  20.  21.            if   (pos >= count) {  22.                return   -  1  ;  23.            }  24.            if   (pos + len > count) {  25.                len = count - pos;  26.            }  27.            if   (len <=   0  ) {  28.                return   0  ;  29.            }  30.            System.arraycopy(buf, pos, b, off, len);  31.            pos += len;  32.            return   len;  33.        }  34.  }  문자열 을 조작 하 는 클래스 StringReader, StringWriter 와 유사 합 니 다. 사실 문자 배열 은 문자열 작업 과 일치 합 니 다. 문자열 은 밑 에 있 는 것 이 실제 적 으로 문자 배열 로 이 루어 집 니 다.StringReader 와 StringWriter 류 를 이해 하면 쉽게 이해 할 수 있 습 니 다.그리고 이 두 가지 유형 도 스 레 드 가 안전 하 다 는 것 을 알려 드 립 니 다.
간단 한 테스트 프로그램 을 만 듭 니 다:
view source print ?
1.  char  [] x={  'a'  ,  'd'  ,  'p'  };  2.  CharArrayWriter cw=  new   CharArrayWriter();  3.  cw.write(x,  0  ,  2  );  4.  cw.append(  "x"  );  5.  System.out.println(cw.toString());   // adx  6.  7.  CharArrayReader cr=  new   CharArrayReader(cw.toCharArray());  8.  System.out.println((  char  )cr.read());  // a  
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.