4.7 字符流的處理
java中提供了處理以16位的Unicode碼表示的字符流的類,即以Reader和Writer 為基類派生出的一系列類。
4.7.1 Reader和Writer
這兩個類是抽象類,只是提供了一系列用于字符流處理的接口,不能生成這兩個類的實例,只能通過使用由它們派生出來的子類對象來處理字符流。
1.Reader類是處理所有字符流輸入類的父類。
讀取字符
public int read() throws IOException; //讀取一個字符,返回值為讀取的字符
public int read(char cbuf[]) throws IOException; /*讀取一系列字符到數(shù)組cbuf[]中,返回值為實際讀取的字符的數(shù)量*/
public abstract int read(char cbuf[],int off,int len) throws IOException;
/*讀取len個字符,從數(shù)組cbuf[]的下標off處開始存放,返回值為實際讀取的字符數(shù)量,該方法必須由子類實現(xiàn)*/來源:www.examda.com
標記流
public boolean markSupported(); //判斷當前流是否支持做標記
public void mark(int readAheadLimit) throws IOException;
//給當前流作標記,最多支持readAheadLimit個字符的回溯。
public void reset() throws IOException; //將當前流重置到做標記處
關(guān)閉流
public abstract void close() throws IOException;
2. Writer類是處理所有字符流輸出類的父類。
向輸出流寫入字符
public void write(int c) throws IOException;
//將整型值c的低16位寫入輸出流
public void write(char cbuf[]) throws IOException;
//將字符數(shù)組cbuf[]寫入輸出流
public abstract void write(char cbuf[],int off,int len) throws IOException;
//將字符數(shù)組cbuf[]中的從索引為off的位置處開始的len個字符寫入輸出流
public void write(String str) throws IOException;
//將字符串str中的字符寫入輸出流
public void write(String str,int off,int len) throws IOException;
//將字符串str 中從索引off開始處的len個字符寫入輸出流
flush( )
刷空輸出流,并輸出所有被緩存的字節(jié)。
關(guān)閉流
public abstract void close() throws IOException;
java中提供了處理以16位的Unicode碼表示的字符流的類,即以Reader和Writer 為基類派生出的一系列類。
4.7.1 Reader和Writer
這兩個類是抽象類,只是提供了一系列用于字符流處理的接口,不能生成這兩個類的實例,只能通過使用由它們派生出來的子類對象來處理字符流。
1.Reader類是處理所有字符流輸入類的父類。
讀取字符
public int read() throws IOException; //讀取一個字符,返回值為讀取的字符
public int read(char cbuf[]) throws IOException; /*讀取一系列字符到數(shù)組cbuf[]中,返回值為實際讀取的字符的數(shù)量*/
public abstract int read(char cbuf[],int off,int len) throws IOException;
/*讀取len個字符,從數(shù)組cbuf[]的下標off處開始存放,返回值為實際讀取的字符數(shù)量,該方法必須由子類實現(xiàn)*/來源:www.examda.com
標記流
public boolean markSupported(); //判斷當前流是否支持做標記
public void mark(int readAheadLimit) throws IOException;
//給當前流作標記,最多支持readAheadLimit個字符的回溯。
public void reset() throws IOException; //將當前流重置到做標記處
關(guān)閉流
public abstract void close() throws IOException;
2. Writer類是處理所有字符流輸出類的父類。
向輸出流寫入字符
public void write(int c) throws IOException;
//將整型值c的低16位寫入輸出流
public void write(char cbuf[]) throws IOException;
//將字符數(shù)組cbuf[]寫入輸出流
public abstract void write(char cbuf[],int off,int len) throws IOException;
//將字符數(shù)組cbuf[]中的從索引為off的位置處開始的len個字符寫入輸出流
public void write(String str) throws IOException;
//將字符串str中的字符寫入輸出流
public void write(String str,int off,int len) throws IOException;
//將字符串str 中從索引off開始處的len個字符寫入輸出流
flush( )
刷空輸出流,并輸出所有被緩存的字節(jié)。
關(guān)閉流
public abstract void close() throws IOException;