Java StringWriter append()方法

java.io.StringWriter.append(CharSequence csq) 方法将指定字符序列到此writer。

1 语法

public StringWriter append(CharSequence csq)

2 参数

csq:要追加的字符序列。如果csq为null,则将四个字符“null”附加到此编写器。

3 返回值

返回Writer。

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.io.StringWriter.append(CharSequence csq)方法的例子
 */
import java.io.*;

public class Demo {
    public static void main(String[] args) {


        // create a new writer
        StringWriter sw = new StringWriter();

        // create two new sequences
        CharSequence sq1 = "Hello";
        CharSequence sq2 = " World";

        // append sequence
        sw.append(sq1);

        // print result
        System.out.println("" + sw.toString());

        // append second sequence
        sw.append(sq2);

        // print result again
        System.out.println("" + sw.toString());

    }
}

输出结果为:

Hello
Hello World

热门文章

优秀文章