Java.io.Writer.write() 方法



描述

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.tutorialspoint;

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
java_io_writer.htm
廣告

© . All rights reserved.