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


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


Java中CharArrayWriter類的writeTo(Writer)方法用於將CharArrayWriter的內容寫入另一個字符流。

public void writeTo(Writer out)
             throws IOException


參數:此方法接受一個代表輸出流的參數,該輸出流是目標流。返回值:此方法不返回任何值。異常:如果發生I /O錯誤,此方法將引發IOException。下麵的程序演示了writeTo(Writer)方法在IO包的CharArrayWriter類中:程序1:

Java

// Java program to illustrate 
// CharArrayWriter writeTo(Writer) method 
  
import java.io.*; 
  
public class GFG { 
    public static void main(String[] args) 
        throws IOException 
    { 
  
        // Create charArrayWriter 
        CharArrayWriter charArrayWriter 
            = new CharArrayWriter(); 
  
        String str = "GEEKS"; 
  
        charArrayWriter.write(str); 
  
        // Create outputStream 
        CharArrayWriter out 
            = new CharArrayWriter(); 
  
        charArrayWriter.writeTo(out); 
  
        // print the outputStream 
        System.out.println( 
            out.toString()); 
    } 
}
輸出:
GEEKS

程序2:

Java

// Java program to illustrate 
// CharArrayWriter writeTo(Writer) method 
  
import java.io.*; 
  
public class GFG { 
    public static void main(String[] args) 
        throws IOException 
    { 
  
        // Create charArrayWriter 
        CharArrayWriter charArrayWriter 
            = new CharArrayWriter(); 
  
        charArrayWriter.write("GEEKSFORGEEKS"); 
  
        // Create outputStream 
        CharArrayWriter out 
            = new CharArrayWriter(); 
  
        charArrayWriter.writeTo(out); 
  
        // print the outputStream 
        System.out.println( 
            out.toString()); 
    } 
}
輸出:
GEEKSFORGEEKS

參考文獻:
https://docs.oracle.com/javase/10/docs/api/java/io/CharArrayWriter.html#writeTo(java.io.Writer)




相關用法


注:本文由純淨天空篩選整理自pp_pankaj大神的英文原創作品 CharArrayWriter writeTo() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。