Java中的CharArrayWriter類的write()方法具有三種類型:
- 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
-
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
-
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)
相關用法
- Java CharArrayWriter writeTo()用法及代碼示例
- Java CharArrayWriter toString()用法及代碼示例
- Java CharArrayWriter flush()用法及代碼示例
- Java CharArrayWriter reset()用法及代碼示例
- Java CharArrayWriter size()用法及代碼示例
- Java CharArrayWriter append()用法及代碼示例
- Java CharArrayWriter toCharArray()用法及代碼示例
- Java CharArrayWriter close()用法及代碼示例
- Java ByteArrayOutputStream write()用法及代碼示例
- Java StringWriter write(int)用法及代碼示例
- Java Writer write(int)用法及代碼示例
- Java PrintWriter write(int)用法及代碼示例
- Java PrintStream write(int)用法及代碼示例
- Java BufferedOutputStream write()用法及代碼示例
- Java BufferedWriter write()用法及代碼示例
- Java StringWriter write(String, int, int)用法及代碼示例
- Java Writer write(char[])用法及代碼示例
- Java PrintStream write(byte[], int, int)用法及代碼示例
- Java Writer write(char[], int, int)用法及代碼示例
- Java StringWriter write(char[], int, int)用法及代碼示例
注:本文由純淨天空篩選整理自pp_pankaj大神的英文原創作品 CharArrayWriter write() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。