当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Java.io.Reader用法及代码示例


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.Reader class in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。