📜  Java Java类

📅  最后修改于: 2022-05-13 01:55:42.645000             🧑  作者: Mango

Java Java类

用于编写过滤字符流的抽象类。抽象类 FilterWriter 本身提供了将所有请求传递给包含的流的默认方法。 FilterWriter 的子类应该覆盖其中的一些方法,并且还可以提供额外的方法和字段。
构造函数:

  • protected FilterWriter(Writer out) :创建一个新的过滤器。

方法 :

  • void close() :关闭流,首先刷新它。一旦流被关闭,进一步的 write() 或 flush() 调用将导致抛出 IOException。关闭以前关闭的流没有效果。
    Syntax :public void close()
               throws IOException
    Throws:
    IOException 
  • void flush() :刷新流。
    Syntax :public void flush()
               throws IOException
    Throws:
    IOException
  • void write(char[] cbuf, int off, int len) :写入字符数组的一部分。
    Syntax :public void write(char[] cbuf,
             int off,
             int len)
               throws IOException
    Parameters:
    cbuf - Buffer of characters to be written
    off - Offset from which to start reading characters
    len - Number of characters to be written
    Throws:
    IOException
  • void write(int c) :写入单个字符。
    Syntax :public void write(int c)
               throws IOException
    Parameters:
    c - int specifying a character to be written
    Throws:
    IOException
  • void write(String str, int off, int len) :写入字符串的一部分。
    Syntax :public void write(String str,
             int off,
             int len)
               throws IOException
    Parameters:
    str - String to be written
    off - Offset from which to start reading characters
    len - Number of characters to be written
    Throws:
    IOException 

程序 :

//Java program demonstrating FilterWriter methods
import java.io.FilterWriter;
import java.io.StringWriter;
import java.io.Writer;
class FilterWriterDemo
{
    public static void main(String[] args) throws Exception
    {
        FilterWriter fr = null;
        Writer wr = null;
        wr = new StringWriter();
        fr = new FilterWriter(wr) {} ;
        String str = "Geeksfor";
        char c[] = {'G','e','e','k'};
  
        //illustrating write(String str,int off,int len)
        fr.write(str);
          
        //illustrating flush()
        fr.flush();
  
        //illustrating write(char[] cff,int off,int len)
        fr.write(c);
  
        //illustrating write(int c)
        fr.write('s');
        System.out.print(wr.toString());
  
        //close the stream
        fr.close();
    }
}

输出 :

GeeksforGeeks