Java中的Java.io.CharArrayWriter类| 2

作者: Arvin Chen 分类: Java 来源: Break易站(www.breakyizhan.com)

Java中的Java.io.CharArrayWriter类| 2

方法:

  • 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流中返回字符数组。
    句法:

    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
  •   本文标题:Java中的Java.io.CharArrayWriter类| 2 - Break易站
    转载请保留页面地址:https://www.breakyizhan.com/java/4289.html

    发表笔记

    电子邮件地址不会被公开。 必填项已用*标注

    更多阅读