Java Java类设置 2
Java中的 CharArrayWriter 类 |设置 1
方法:
- append (char Sw) : Java.io.CharArrayWriter.append(char Sw)将单个字符附加到 Writer。
句法:public CharArrayWriter append(char Sw) Parameters : Sw : character to be append Return : CharArrayWriter
- append (CharSequence char_sq) : Java.io.CharArrayWriter.append(CharSequence char_sq)将指定的字符序列附加到 Writer。
句法:public CharArrayWriter append(CharSequence char_sq) Parameters : char_sq : Character sequence to append. Return : CharArrayWriter, if char sequence is null, then NULL appends to the Writer.
- append (CharSequence char_sq, int start, int end) : Java.io.CharArrayWriter.append(CharSequence char_sq, int start, int end)将字符序列的指定部分附加到 Writer。
句法:public CharArrayWriter append(CharSequence char_sq, int start, int end) Parameters : char_sq : Character sequence to append. start : start of character in the Char Sequence end : end of character in the Char Sequence Return : void
- flush() : Java.io.CharArrayWriter.flush()刷新 Writer 流
句法:public void flush() Parameters : ----- Return : void
- toCharArray() : Java.io.CharArrayWriter.toCharArray()从 Writer Stream 返回字符数组。
句法:public char[] toCharArray() Parameters : ----- Return : void
- reset() : Java.io.CharArrayWriter.reset()重置 Writer 缓冲区,以便它可以再次使用。
句法:public void reset() Parameters : ----- Return : void
说明 CharArrayWriter 类方法使用的Java程序:
// Java program illustrating the working of CharArrayWriter class methods // append(CharSequence char_sq), append(char Sw) // append(CharSequence char_sq, int start,int end) // flush(), reset(), toCharArray import java.io.*; public class NewClass { public static void main(String[] args) throws IOException { // Initializing String Witer CharArrayWriter geek_writer1 = new CharArrayWriter(); CharArrayWriter geek_writer2 = new CharArrayWriter(); CharArrayWriter geek_writer3 = new CharArrayWriter(); char[] Sw = {'G','E','E','K','S'}; for(char c: Sw) { // Use of append(char Sw) : geek_writer1 .append(c); System.out.println("append(char Sw) :" + geek_writer1 .toString()); } // Using flush() method geek_writer1.flush(); System.out.println("\nUsing flush() : "+ geek_writer1.toString()); System.out.println(""); // Initializing Character Sequence CharSequence char_sq1 = "1 Hello 1"; CharSequence char_sq2 = " : 2 Geeks 2"; // Use of append(CharSequence char_sq) geek_writer2.append(char_sq1); geek_writer2.append(char_sq2); System.out.println("append(char_sq) : " + geek_writer2.toString()); // Use of append(CharSequence char_sq,int start,int end) geek_writer3.append(char_sq1, 0, 3); geek_writer3.append(char_sq2, 3, 6); System.out.println("append(char_sq,start,end) : " + geek_writer3.toString()); System.out.println(""); // Use of toCharArray() : char[] toChar1 = geek_writer1.toCharArray(); for (char c1 : toChar1) { System.out.println("toCharArray : "+ c1); } geek_writer1.reset(); System.out.println("\nReset is invoked"); char[] toChar2 = geek_writer1.toCharArray(); for (char c2 : toChar2) { System.out.println("toCharArray : "+ c2); } System.out.println("\ntoCharArray not working as reset is invoked"); } }
输出 :
append(char Sw) :G append(char Sw) :GE append(char Sw) :GEE append(char Sw) :GEEK append(char Sw) :GEEKS Using flush() : GEEKS append(char_sq) : 1 Hello 1 : 2 Geeks 2 append(char_sq,start,end) : 1 H2 G toCharArray : G toCharArray : E toCharArray : E toCharArray : K toCharArray : S Reset is invoked toCharArray not working as reset is invoked