Java.io.CharArrayWriter.write() 方法



描述

java.io.BufferedInputStream.write(String str, int off, int len) 方法將字串的一部分寫入寫入器。

宣告

以下是 java.io.CharArrayWriter.write(String str, int off, int len) 方法的宣告:

public void write(String str, int off, int len)

引數

  • str - 源字串。

  • off - 開始讀取字元的偏移量。

  • len - 要寫入的字元數。

返回值

此方法不返回值。

異常

示例

以下示例演示了 java.io.CharArrayWriter.write(String str, int off, int len) 方法的用法。

package com.tutorialspoint;

import java.io.CharArrayWriter;

public class CharArrayWriterDemo {
   public static void main(String[] args) {      
      String str = "Hello World!!";
      CharArrayWriter chw = null;
            
      try {
         // create character array writer
         chw = new CharArrayWriter();

         // portion to be written to writer
         chw.write(str, 4, 9);
         
         // print the buffer as string
         System.out.println(chw.toString());
               
      } catch(Exception e) {
         // for any error
         e.printStackTrace();
      } finally {
         // releases all system resources from writer
         if(chw!=null)
            chw.close();
      }
   }
}

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

o World!!
java_io_chararraywriter.htm
廣告