java.io.Writer.write(char[] cbuf,int off,int len)方法範例


java.io.Writer.write(char[] cbuf,int off,int len) 方法寫入字元陣列的一部分。

宣告

以下是java.io.Writer.write()方法的宣告

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

引數

  • cbuf -- 寫入的字元陣列

  • off -- 從開始寫入字元的偏移量

  • len -- 要寫入的字元數

返回值

此方法沒有返回值

異常

  • IOException -- 如果發生I/ O錯誤

例子

下面的範例演示java.io.Writer.write()方法的用法。

package com.yiibai;

import java.io.*;

public class WriterDemo {

   public static void main(String[] args) {
      char[] c = {'h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd'};

      // create a new writer
      Writer writer = new PrintWriter(System.out);

      try {
         // write a portion of a char array
         writer.write(c, 0, 5);

         // flush the writer
         writer.flush();

         // write another portion of a char array
         writer.write(c, 5, 5);

         // flush the stream again
         writer.close();

      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

讓我們來編譯和執行上面的程式,這將產生以下結果:

helloworld