當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Java.io.CharArrayWriter.writeTo()用法及代碼示例



描述

這個java.io.BufferedInputStream.writeTo(Writer out)方法將緩衝區內容寫入另一個流。

聲明

以下是聲明java.io.CharArrayWriter.writeTo(Writer out)方法 -

public void writeTo(Writer out)

參數

out− 目標輸出流

返回值

此方法不返回任何值。

異常

IOException− 如果發生任何 IO 錯誤。

示例

下麵的例子展示了 java.io.CharArrayWriter.writeTo(Writer out) 方法的用法。

package com.tutorialspoint;

import java.io.CharArrayWriter;

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

         // create destination character array writer
         chwd = new CharArrayWriter();
         
         // string to be written to writer
         chw.write(str);
         
         // write to destination array writer
         chw.writeTo(chwd);
         
         // print the destination buffer content as string
         System.out.println(chwd.toString());
               
      } catch(Exception e) {
         // for any error
         e.printStackTrace();
      } finally {
         // releases all system resources from writer
         if(chw!=null)
            chw.close();
      }
   }
}

讓我們編譯並運行上麵的程序,這將產生以下結果 -

Hello World!!

相關用法


注:本文由純淨天空篩選整理自 Java.io.CharArrayWriter.writeTo() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。