Java.io.FilterWriter.write() 方法



描述

java.io.FilterWriter.write(char[] cbuf, int off, int len) 方法將 len 個字元的一部分寫入檔案寫入器,從字元陣列的偏移量 off 讀取。

宣告

以下是 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
java_io_filterwriter.htm
廣告

© . All rights reserved.