Java Java类
它是一个用于读取字符流的抽象类。子类必须实现的唯一方法是 read(char[], int, int) 和 close()。然而,大多数子类将覆盖此处定义的一些方法,以提供更高的效率、附加功能或两者兼而有之。
构造函数:
- protected Reader() :创建一个新的字符流阅读器,其关键部分将在阅读器本身上同步。
- protected Reader(Object lock):创建一个新的字符流读取器,其关键部分将在给定对象上同步。
方法:
- abstract void close() :关闭流并释放与之关联的任何系统资源。关闭流后,进一步的 read()、ready()、mark()、reset() 或 skip() 调用将引发 IOException。关闭以前关闭的流没有效果。
Syntax :public abstract void close() throws IOException Throws: IOException
- void mark(int readAheadLimit) :标记流中的当前位置。对 reset() 的后续调用将尝试将流重新定位到该点。并非所有字符输入流都支持 mark() 操作。
Syntax :public void mark(int readAheadLimit) throws IOException Parameters: 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
- boolean markSupported() :判断此流是否支持 mark() 操作。默认实现总是返回 false。子类应覆盖此方法。
Syntax :public boolean markSupported() Returns: true if and only if this stream supports the mark operation.
- int read() :读取单个字符。此方法将阻塞,直到字符可用、发生 I/O 错误或到达流的末尾。
打算支持有效的单字符输入的子类应覆盖此方法。Syntax :public int read() throws IOException Returns: 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
- int read(char[] cbuf) :将字符读入数组。此方法将阻塞,直到某些输入可用、发生 I/O 错误或到达流的末尾。
Syntax :public int read(char[] cbuf) throws IOException Parameters: cbuf - Destination buffer Returns: The number of characters read, or -1 if the end of the stream has been reached Throws: IOException
- abstract int read(char[] cbuf, int off, int len) :将字符读入数组的一部分。此方法将阻塞,直到某些输入可用、发生 I/O 错误或到达流的末尾.
Syntax :public abstract int read(char[] cbuf, int off, int len) throws IOException Parameters: cbuf - Destination buffer off - Offset at which to start storing characters len - Maximum number of characters to read Returns: The number of characters read, or -1 if the end of the stream has been reached Throws: IOException
- int read(CharBuffer target) :尝试将字符读入指定的字符缓冲区。缓冲区按原样用作字符的存储库:所做的唯一更改是 put 操作的结果。不执行缓冲区的翻转或倒带。
Syntax :public int read(CharBuffer target) throws IOException Parameters: target - the buffer to read characters into Returns: The number of characters added to the buffer, or -1 if this source of characters is at its end Throws: IOException NullPointerException ReadOnlyBufferException
- boolean ready() :判断该流是否准备好被读取。
Syntax :public boolean ready() throws IOException Returns: 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
- void reset() :重置流。如果流已被标记,则尝试将其重新定位在标记处。如果流尚未被标记,则尝试以适合特定流的某种方式将其重置,例如将其重新定位到其起点。并非所有字符输入流都支持 reset() 操作,有些支持 reset() 但不支持 mark()。
Syntax :public void reset() throws IOException Throws: IOException
- long skip(long n) :跳过字符。此方法将阻塞,直到某些字符可用、发生 I/O 错误或到达流的末尾。
Syntax :public long skip(long n) throws IOException Parameters: n - The number of characters to skip Returns: The number of characters actually skipped Throws: IllegalArgumentException - If n is negative. IOException
//Java program demonstrating Reader methods
import java.io.*;
import java.nio.CharBuffer;
import java.util.Arrays;
class ReaderDemo
{
public static void main(String[] args) throws IOException
{
Reader r = new FileReader("file.txt");
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");
}
//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