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


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


用于读取过滤字符流的抽象类。抽象类FilterReader本身提供了将所有请求传递到所包含流的默认方法。 FilterReader 的子类应该重写其中一些方法,并且还可以提供其他方法和字段。
构造函数:

  • protected FilterReader(读入):创建一个新的过滤阅读器。

方法:

  • 无效close():关闭流并释放与其关联的所有系统资源。关闭流后,进一步的 read()、ready()、mark()、reset() 或 skip() 调用将引发 IOException。关闭之前关闭的流没有任何效果。
    Syntax :public void close()
               throws IOException
    Throws:
    IOException
  • 无效标记(int readAheadLimit):标记流中的当前位置。
    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
  • 布尔值markSupported():告知此流是否支持mark()操作。
    Syntax :public boolean markSupported()
    返回:
    true if and only if this stream supports the mark operation.
  • int read():读取单个字符。
    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 读取(char[] cbuf,int 关闭,int len):将字符读入数组的一部分。
    Syntax :public 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
  • 布尔值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
  • 无效reset():重置流。
    Syntax :public void reset()
               throws IOException
    Throws:
    IOException
  • 长跳过(长n):跳过字符。
    Syntax :public long skip(long n)
              throws IOException
    参数:
    n - The number of characters to skip
    返回:
    The number of characters actually skipped
    Throws:
    IOException

程序:


//Java program illustrating FilterReader class methods 
  
import java.io.*; 
class FilterReaderdemo 
{ 
    public static void main(String[] args) throws IOException 
    { 
        Reader r = new StringReader("GeeksforGeeks"); 
        FilterReader fr = new FilterReader(r)  
        { 
        }; 
        char ch[] = new char[8]; 
          
        //illustrating markSupported() 
        if(fr.markSupported()) 
        { 
            //illustrating mark() method 
            System.out.println("mark method is supported"); 
            fr.mark(100); 
        } 
          
        //illustrating skip() method 
        fr.skip(5); 
          
        //illustrating ready() 
        if(fr.ready()) 
        { 
            //illustrating read(char[] cbuff,int off,int len) 
            fr.read(ch); 
            for (int i = 0; i < 8; i++)  
            { 
                System.out.print(ch[i]); 
            } 
              
            //illustrating reset() 
            fr.reset(); 
            for (int i = 0; i <5 ; i++) 
            { 
                //illustrating read() 
                System.out.print((char)fr.read()); 
            } 
        } 
          
        //closing the stream 
        fr.close(); 
    } 
} 

输出:

mark method is supported
forGeeksGeeks


相关用法


注:本文由纯净天空筛选整理自佚名大神的英文原创作品 Java.io.FilterReader class in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。