📜  Java中的 CharArrayWriter append() 方法及示例

📅  最后修改于: 2022-05-13 01:55:37.467000             🧑  作者: Mango

Java中的 CharArrayWriter append() 方法及示例

Java中CharArrayWriter类的append()方法分为三种:

  1. Java中CharArrayWriter类的append(char)方法用于将指定的字符追加到 writer。此 append() 方法一次将一个字符附加到 CharArrayWriter 并返回此 CharArrayWriter。
    句法:
    public CharArrayWriter append(char c)
    
    

    指定者:该方法由Appendable接口的 append() 方法指定。
    覆盖:此方法覆盖Writer类的 append() 方法。
    参数:此方法接受一个参数c ,该参数表示要附加的 16 位字符。
    返回值:该方法在追加字符后返回CharArrayWriter
    异常:此方法不会抛出任何异常。
    下面的程序说明了 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());
        }
    }


    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());
        }
    }


    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
    
  2. Java中CharArrayWriter类的append(CharSequence)方法用于将指定的字符序列追加到写入器。此 append() 方法一次将一个字符序列附加到 CharArrayWriter 并返回此 CharArrayWriter。
    句法:

    public CharArrayWriter append(CharSequence csq)
    

    指定者:该方法由Appendable接口的 append() 方法指定。
    覆盖:此方法覆盖Writer类的 append() 方法。
    参数:此方法接受一个参数csq ,表示要附加的字符序列。如果字符序列为空,则将 4 个字符“空”附加到 CharArrayWriter。
    返回值:该方法在追加字符序列后返回CharArrayWriter
    异常:此方法不会抛出任何异常。
    下面的程序说明了 IO 包中 CharArrayWriter 类中的 append(CharSequence) 方法:
    程序:

    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)方法用于将指定字符序列的子序列追加到写入器。此 append() 方法将部分字符序列附加到 CharArrayWriter 并返回此 CharArrayWriter。
    句法:

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

    指定者:该方法由Appendable接口的 append() 方法指定。
    覆盖:此方法覆盖Writer类的 append() 方法。
    参数:此方法接受三个参数:

    • csq - 它表示要附加其子序列的字符序列。
    • start - 它表示子序列的起始索引。
    • end - 它表示子序列结尾之后的字符索引。

    返回值:此方法在将给定字符序列的子序列附加到其中后返回CharArrayWriter
    异常:如果 start 或 end 为负数或 start 大于 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 Java )
2. Java Java )
3. https://docs.oracle.com/javase/10/docs/api/java Java Java, int, int)