JAVA伊奥。Java中的Reader类

它是一个用于读取字符流的抽象类。子类必须实现的唯一方法是read(char[],int,int)和close()。然而,大多数子类将重写此处定义的一些方法,以提供更高的效率和/或附加功能。 施工人员:

null
  • 受保护的读取器(): 创建一个新的字符流读取器,其关键部分将在读取器本身上同步。
  • 受保护的读卡器(对象锁): 创建一个新的字符流读取器,其关键部分将在给定对象上同步。

方法:

  • 抽象void close(): 关闭流并释放与之关联的所有系统资源。流关闭后,进一步的read()、ready()、mark()、reset()或skip()调用将抛出IOException。关闭以前关闭的流没有效果。
    Syntax :public abstract void close()
                        throws IOException
    Throws:
    IOException 
  • 无效标记(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 
  • 支持布尔标记() 说明此流是否支持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(字符[]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 
  • 抽象整数读取(字符[]cbuf,整数关闭,整数长度): 将字符读入数组的一部分。此方法将一直阻止,直到某些输入可用、发生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(字符缓冲区目标): 尝试将字符读入指定的字符缓冲区。缓冲区按原样用作字符存储库:所做的唯一更改是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
  • 布尔就绪() 指示此流是否已准备好读取。
    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
  • 长跳跃(长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

本文由 尼森特·夏尔马 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享