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

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

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

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

  1. Java中CharArrayWriter类的write(int)方法用于将字符以整数形式写入 writer。此 write() 方法一次将一个字符写入 CharArrayWriter。
    句法:
    public void write(int c)
    
    

    覆盖:此方法覆盖Writer类的 write() 方法。
    参数:此方法接受一个参数c ,该参数表示要写入的整数。
    返回值:此方法不返回任何值。
    异常:此方法不会抛出任何异常。
    下面的程序说明了 IO 包中 CharArrayWriter 类中的 write(int) 方法:
    程序:

    Java
    // Java program to illustrate
    // CharArrayWriter write(int) method
      
    import java.io.*;
      
    public class GFG {
        public static void main(String[] args)
        {
      
            // Create charArrayWriter
            CharArrayWriter charArrayWriter
                = new CharArrayWriter();
      
            // Write the character
            charArrayWriter.write(71);
      
            charArrayWriter.write(69);
      
            charArrayWriter.write(69);
      
            charArrayWriter.write(75);
      
            charArrayWriter.write(83);
      
            // print the charArrayWriter
            System.out.println(
                charArrayWriter.toString());
        }
    }


    Java
    // Java program to illustrate
    // CharArrayWriter
    // write(char[], int, int) method
      
    import java.io.*;
      
    public class GFG {
        public static void main(String[] args)
        {
      
            // Create charArrayWriter
            CharArrayWriter charArrayWriter
                = new CharArrayWriter();
      
            // Create character array
            char[] c = { 'G', 'E', 'E', 'K', 'S' };
      
            // Write protion of array
            // to the charArrayWriter
            charArrayWriter.write(c, 0, 4);
      
            // print the charArrayWriter
            System.out.println(
                charArrayWriter.toString());
        }
    }


    Java
    // Java program to illustrate
    // CharArrayWriter
    // write(String, int, int) method
      
    import java.io.*;
      
    public class GFG {
        public static void main(String[] args)
        {
      
            // Create charArrayWriter
            CharArrayWriter charArrayWriter
                = new CharArrayWriter();
      
            // Create string
            String str = "GEEKSFORGEEKS";
      
            // Write protion of string
            // to the charArrayWriter
            charArrayWriter.write(str, 8, 5);
      
            // print the charArrayWriter
            System.out.println(
                charArrayWriter.toString());
        }
    }


    输出:
    GEEKS
    
  2. Java中CharArrayWriter类的write(char[], int, int)方法用于向 writer 写入字符。它将给定字符数组的一部分写入 CharArrayWriter。
    句法:

    public void write(char[] c,
                      int offset,
                      int length)
    

    指定者:该方法由Writer类的 write() 方法指定。
    参数:此方法接受三个参数:

    • c – 它表示字符数组,其中要写入的部分。
    • offset – 它表示字符数组中的起始索引。
    • length - 它表示要写入的字符数。

    返回值:此方法不返回任何值。
    异常:如果 offset 为负数、length 为负数、offset 和 length 之和为负数或大于给定字符数组的长度,则此方法抛出IndexOutOfBoundsException
    下面的程序说明了 IO 包中 CharArrayWriter 类中的 write(char[], int, int) 方法:
    程序:

    Java

    // Java program to illustrate
    // CharArrayWriter
    // write(char[], int, int) method
      
    import java.io.*;
      
    public class GFG {
        public static void main(String[] args)
        {
      
            // Create charArrayWriter
            CharArrayWriter charArrayWriter
                = new CharArrayWriter();
      
            // Create character array
            char[] c = { 'G', 'E', 'E', 'K', 'S' };
      
            // Write protion of array
            // to the charArrayWriter
            charArrayWriter.write(c, 0, 4);
      
            // print the charArrayWriter
            System.out.println(
                charArrayWriter.toString());
        }
    }
    
    输出:
    GEEK
    
  3. Java中CharArrayWriter类的write(String, int, int)方法用于将字符串的一部分写入写入器。此 write() 方法将给定字符串的一部分写入 CharArrayWriter。
    句法:

    public void write(String str,
                      int offset,
                      int length)
    
    

    覆盖:此方法覆盖Writer类的 write() 方法。
    参数:此方法接受三个参数:

    • str - 它表示字符串,其中的一部分是要写入的。
    • offset – 它表示字符串中的起始索引。
    • length - 它表示要写入的字符数。

    返回值:此方法不返回任何值。
    异常:如果 offset 为负数,或者 length 为负数,或者 offset 和 length 之和为负数或大于给定字符串的长度,则此方法抛出IndexOutOfBoundsException
    下面的程序说明了 IO 包中 CharArrayWriter 类中的 write(String, int, int) 方法:
    程序:

    Java

    // Java program to illustrate
    // CharArrayWriter
    // write(String, int, int) method
      
    import java.io.*;
      
    public class GFG {
        public static void main(String[] args)
        {
      
            // Create charArrayWriter
            CharArrayWriter charArrayWriter
                = new CharArrayWriter();
      
            // Create string
            String str = "GEEKSFORGEEKS";
      
            // Write protion of string
            // to the charArrayWriter
            charArrayWriter.write(str, 8, 5);
      
            // print the charArrayWriter
            System.out.println(
                charArrayWriter.toString());
        }
    }
    
    输出:
    GEEKS
    

参考:
1. https://docs.oracle.com/javase/10/docs/api/java Java)
2. https://docs.oracle.com/javase/10/docs/api/java Java, int, int)
3. https://docs.oracle.com/javase/10/docs/api/java Java Java, int, int)