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


Java CharArrayWriter write()用法及代码示例


Java中的CharArrayWriter类的write()方法具有三种类型:

  1. Java中CharArrayWriter类的write(int)方法用于以整数形式向写入器写入字符。此write()方法一次将一个字符写入CharArrayWriter.Syntax:
    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()); 
        } 
    }
    输出:
    GEEKS
    
  2. Java中CharArrayWriter类的write(char [],int,int)方法用于将字符写入编写器。它将给定字符数组的一部分写入CharArrayWriter.Syntax:



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

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

    • c-它代表字符数组,其中一部分要写入。
    • 抵消-它表示字符数组中的起始索引。
    • 长度-它表示要写入的字符数。

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

    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.Syntax:

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

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

    • 力量-它表示要写入其一部分的字符串。
    • 抵消-它表示字符串中的起始索引。
    • 长度-它表示要写入的字符数。

    返回值:此方法不返回任何值。
    异常:如果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/io/CharArrayWriter.html#write(int)
2. https://docs.oracle.com/javase/10/docs/api/java/io/CharArrayWriter.html#write(char%5B%5D, int, int)
3. https://docs.oracle.com/javase/10/docs/api/java/io/CharArrayWriter.html#write(java.lang.String, int, int)




相关用法


注:本文由纯净天空筛选整理自pp_pankaj大神的英文原创作品 CharArrayWriter write() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。