Java Reader類是一個用於讀取字符流的抽象類。子類必須實現的唯一方法是 read(char[], int, int) 和 close()。然而,大多數子類將重寫此處定義的一些方法,以提供更高的效率和/或附加函數。
Java Reader 類中的構造函數
Java Reader 類使用兩個構造函數,如下所述:
- 受保護Reader():創建一個新的字符流讀取器,其關鍵部分將在讀取器本身上同步。
- 受保護的讀卡器(對象鎖):創建一個新的字符流讀取器,其關鍵部分將在給定對象上同步。
Java Reader 類中的方法
1.抽象無效close()
關閉流並釋放與其關聯的所有係統資源。流關閉後,進一步的 read()、ready()、mark()、reset() 或 skip() 調用將拋出 IOException。關閉之前關閉的流沒有任何效果。
Syntax: public abstract void close() throws IOException Throws: IOException
2. 無效標記(int readAheadLimit)
標記流中的當前位置。後續調用 reset() 將嘗試將流重新定位到該點。並非所有character-input流都支持mark()操作。
Syntax: public void mark(int readAheadLimit) throws IOException 參數: readAheadLimit - Limit on the number of characters that may be read while still preserving the mark. After reading this many characters, attempting to reset the stream may fail. Throws: IOException
3.布爾值markSupported()
告知此流是否支持mark()操作。默認實現始終返回 false。子類應該重寫此方法。
Syntax :public boolean markSupported() 返回: true if and only if this stream supports the mark operation.
4.int read()
讀取單個字符。此方法將阻塞,直到有字符可用、發生 I/O 錯誤或到達流末尾。想要支持高效單字符輸入的子類應該重寫此方法。
Syntax :public int read() throws IOException 返回: The character read, as an integer in the range 0 to 65535 (0x00-0xffff), or -1 if the end of the stream has been reached Throws: IOException
5.int讀取(char[] cbuf)
將字符讀入數組。此方法將阻塞,直到某些輸入可用、發生 I/O 錯誤或到達流末尾。
Syntax :public int read(char[] cbuf) throws IOException 參數: cbuf - Destination buffer 返回: The number of characters read, or -1 if the end of the stream has been reached Throws: IOException
6. 抽象 int 讀取(char[] cbuf, int off, int len)
將字符讀入數組的一部分。此方法將阻塞,直到某些輸入可用、發生 I/O 錯誤或到達流末尾。
Syntax :public abstract int read(char[] cbuf,int off,int len) throws IOException 參數: cbuf - Destination buffer off - Offset at which to start storing characters len - Maximum number of characters to read 返回: The number of characters read, or -1 if the end of the stream has been reached Throws: IOException
7. int 讀取(CharBuffer 目標)
嘗試將字符讀入指定的字符緩衝區。緩衝區按原樣用作字符存儲庫:所做的唯一更改是 put 操作的結果。不執行緩衝區的翻轉或倒帶。
Syntax :public int read(CharBuffer target) throws IOException 參數: target - the buffer to read characters into 返回: The number of characters added to the buffer, or -1 if this source of characters is at its end Throws: IOException NullPointerException ReadOnlyBufferException
8. 布爾值ready()
告知該流是否已準備好讀取。
Syntax :public boolean ready() throws IOException 返回: True if the next read() is guaranteed not to block for input, false otherwise. Note that returning false does not guarantee that the next read will block. Throws: IOException
9.無效reset()
重置流。如果流已被標記,則嘗試將其重新定位在標記處。如果流尚未被標記,則嘗試以適合特定流的某種方式重置它,例如通過將其重新定位到其起始點。並非所有character-input流都支持reset()操作,有些流支持reset()而不支持mark()。
Syntax :public void reset() throws IOException Throws: IOException
10. 長跳過(long n)
跳過字符。此方法將阻塞,直到某些字符可用、發生 I/O 錯誤或到達流末尾。
Syntax :public long skip(long n) throws IOException 參數: n - The number of characters to skip 返回: The number of characters actually skipped Throws: IllegalArgumentException - If n is negative. IOException
示例
Java
// Java program demonstrating Reader methods
import java.io.*;
import java.nio.CharBuffer;
import java.util.Arrays;
// Driver Class
class ReaderDemo {
// Main function
public static void main(String[] args)
throws IOException
{
Reader r = new FileReader(" file.txt & quot;);
PrintStream out = System.out;
char c[] = new char[10];
CharBuffer cf = CharBuffer.wrap(c);
// illustrating markSupported()
if (r.markSupported()) {
// illustrating mark()
r.mark(100);
out.println("
mark method is supported & quot;);
}
// skipping 5 characters
r.skip(5);
// checking whether this stream is ready to be read.
if (r.ready()) {
// illustrating read(char[] cbuf,int off,int
// len)
r.read(c, 0, 10);
out.println(Arrays.toString(c));
// illustrating read(CharBuffer target )
r.read(cf);
out.println(Arrays.toString(cf.array()));
// illustrating read()
out.println((char)r.read());
}
// closing the stream
r.close();
}
}
輸出
[f, g, h, i, g, k, l, m, n, o] [p, q, r, s, t, u, v, w, x, y] z
Reader類的實現
下麵提到了 Java 中 Reader 類的一些實現:
- Java.io.BufferedReader
- Java.io.CharArrayReader
- Java.io.FilterReader
- InputStreamReader
- Java.io.PipedReader
- Java.io.StringReader
相關用法
- Java Java.io.Reader.close()用法及代碼示例
- Java Java.io.Reader.mark()用法及代碼示例
- Java Java.io.Reader.markSupported()用法及代碼示例
- Java Java.io.Reader.read()用法及代碼示例
- Java Java.io.Reader.ready()用法及代碼示例
- Java Java.io.Reader.reset()用法及代碼示例
- Java Java.io.Reader.skip()用法及代碼示例
- Java Java.io.RandomAccessFile.close()用法及代碼示例
- Java Java.io.RandomAccessFile.getChannel()用法及代碼示例
- Java Java.io.RandomAccessFile.getFD()用法及代碼示例
- Java Java.io.RandomAccessFile.getFilePointer()用法及代碼示例
- Java Java.io.RandomAccessFile.length()用法及代碼示例
- Java Java.io.RandomAccessFile.read()用法及代碼示例
- Java Java.io.RandomAccessFile.readBoolean()用法及代碼示例
- Java Java.io.RandomAccessFile.readByte()用法及代碼示例
- Java Java.io.RandomAccessFile.readChar()用法及代碼示例
- Java Java.io.RandomAccessFile.readDouble()用法及代碼示例
- Java Java.io.RandomAccessFile.readFloat()用法及代碼示例
- Java Java.io.RandomAccessFile.readFully()用法及代碼示例
- Java Java.io.RandomAccessFile.readInt()用法及代碼示例
- Java Java.io.RandomAccessFile.readLine()用法及代碼示例
- Java Java.io.RandomAccessFile.readLong()用法及代碼示例
- Java Java.io.RandomAccessFile.readShort()用法及代碼示例
- Java Java.io.RandomAccessFile.readUTF()用法及代碼示例
- Java Java.io.RandomAccessFile.seek()用法及代碼示例
注:本文由純淨天空篩選整理自佚名大神的英文原創作品 Java.io.Reader class in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。