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


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。