当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。