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


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


Java中的CharArrayWriter類的append()方法具有三種類型:

  1. Java中CharArrayWriter類的append(char)方法用於將指定的字符附加到writer。此append()方法一次將一個字符追加到CharArrayWriter並返回此CharArrayWriter.Syntax:
    public CharArrayWriter append(char c)
    
    

    指定者:此方法由Appendable接口的append()方法指定重寫:此方法重寫Writer類的append()方法參數:此方法接受一個參數c表示要附加的16位字符。異常:此方法不會引發任何異常。以下示例程序旨在說明IO包中CharArrayWriter類中的append(char)方法:

    Java

    // Java program to illustrate 
    // CharArrayWriter append(char) method 
      
    import java.io.*; 
      
    public class GFG { 
        public static void main(String[] args) 
        { 
      
            // Create charArrayWriter 
            CharArrayWriter charArrayWriter 
                = new CharArrayWriter(); 
      
            // Append the character 
            charArrayWriter.append('G'); 
      
            charArrayWriter.append('E'); 
      
            charArrayWriter.append('E'); 
      
            charArrayWriter.append('K'); 
      
            charArrayWriter.append('S'); 
      
            // print the charArrayWriter 
            System.out.println( 
                charArrayWriter.toString()); 
        } 
    }
    輸出:
    GEEKS
    
  2. Java中CharArrayWriter類的append(CharSequence)方法用於將指定的字符序列附加到writer。此append()方法一次將一個字符序列附加到CharArrayWriter並返回此CharArrayWriter.Syntax:



    public CharArrayWriter append(CharSequence csq)
    

    指定者:此方法由Appendable接口的append()方法指定。替代:此方法重寫Writer類的append()方法。參數:該方法接受一個參數csq,該參數表示要附加的字符序列。如果字符序列為空,則將4個字符‘null’附加到CharArrayWriter中。返回值:該方法在將字符序列附加到其後返回CharArrayWriter.Exceptions:該方法不引發任何異常。以下示例程序旨在說明append(CharSequence)方法在IO包的CharArrayWriter類中:程序:

    Java

    // Java program to illustrate 
    // CharArrayWriter append(CharSequence) method 
      
    import java.io.*; 
      
    public class GFG { 
        public static void main(String[] args) 
        { 
      
            // Create charArrayWriter 
            CharArrayWriter charArrayWriter 
                = new CharArrayWriter(); 
      
            // Create character sequence 
            CharSequence csq1 = "GEEKS"; 
      
            CharSequence csq2 = "FOR"; 
      
            // Append character sequences 
            // to the charArrayWriter 
            charArrayWriter.append(csq1); 
      
            charArrayWriter.append(csq2); 
      
            charArrayWriter.append(csq1); 
      
            // print the charArrayWriter 
            System.out.println( 
                charArrayWriter.toString()); 
        } 
    }
    輸出:
    GEEKSFORGEEKS
    
  3. Java中CharArrayWriter類的append(CharSequence,int,int)方法用於將指定字符序列的子序列追加到writer。此append()方法將字符序列的一部分附加到CharArrayWriter並返回此CharArrayWriter.Syntax:

    public CharArrayWriter append(CharSequence csq, 
                                  int start, 
                                  int end)
    
    

    指定者:此方法由Appendable接口的append()方法指定。重寫:此方法重寫Writer類的append()方法參數:此方法接受三個參數:

    • csq-它表示要附加其子序列的字符序列。
    • 開始-它代表子序列的起始索引。
    • 結束-它表示子序列結尾之後的字符索引。

    返回值:異常:此方法在將給定字符序列的子序列追加到其中之後返回CharArrayWriter.Exceptions:如果開始或結尾為負或開始大於end或end大於給定字符序列的長度,則此方法引發IndexOutOfBoundsException。在IO程序包的CharArrayWriter類中說明了append(CharSequence,int,int)方法:

    Java

    // Java program to illustrate 
    // CharArrayWriter 
    // append(CharSequence, int, int) method 
      
    import java.io.*; 
      
    public class GFG { 
        public static void main(String[] args) 
        { 
      
            // Create charArrayWriter 
            CharArrayWriter charArrayWriter 
                = new CharArrayWriter(); 
      
            // Create character sequence 
            CharSequence csq = "GEEKSFORGEEKS"; 
      
            // Append subsequence of character 
            // sequence to the charArrayWriter 
            charArrayWriter.append(csq, 8, 13); 
      
            // print the charArrayWriter 
            System.out.println( 
                charArrayWriter.toString()); 
        } 
    }
    輸出:
    GEEKS
    

參考文獻:
1. https://docs.oracle.com/javase/10/docs/api/java/io/CharArrayWriter.html#append(char)
2. https://docs.oracle.com/javase/10/docs/api/java/io/CharArrayWriter.html#append(java.lang.CharSequence)
3. https://docs.oracle.com/javase/10/docs/api/java/io/CharArrayWriter.html#append(java.lang.CharSequence, int, int)




相關用法


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