Java.io.FilterWriter.write() 方法

描述

java.io.FilterWriter.write(char[] cbuf, int off, int len) 方法将 len 个字符的一部分写入文件写入器,read() 从 字符数组。


声明

以下是 java.io.FilterWriter.write(char[] cbuf, int off, int len) 方法的声明 −

public void write(char[] cbuf, int off, int len)

参数

  • cbuf − 要写入此过滤器编写器的源字符缓冲区。

  • off − 开始读取字符的偏移量。

  • len − 要写入的字符数。


返回值

该方法不返回任何值。


异常

IOException − 如果发生 I/O 错误。


示例

下面的例子展示了 java.io.FilterWriter.write(char[] cbuf, int off, int len) 方法的使用。

package com.tutorialspoint;

import java.io.FilterWriter;
import java.io.StringWriter;
import java.io.Writer;

public class FilterWriterDemo {
   public static void main(String[] args) throws Exception {
      FilterWriter fw = null;
      Writer w = null;
      char[] cbuf = {'A','B','C','D','E','F'};
      String s = null;
      
      try {
         // create new reader
         w = new StringWriter(6);
          
         // filter writer
         fw = new FilterWriter(w) {
         };
         
         // write to filter writer from character buffer
         fw.write(cbuf, 2, 4);

         // get the string
         s = w.toString();
         
         // print
         System.out.print("String: "+s);
         
      } catch(Exception e) {
         // if any I/O error occurs
         e.printStackTrace();
      } finally {
         // releases system resources associated with this stream
         if(w!=null)
            w.close();
         if(fw!=null)
            fw.close();
      }
   }
}

让我们编译并运行上面的程序,这将产生下面的结果 −

String: CDEF