原文: https://beginnersbook.com/2015/05/java-writer-to-string-conversion/

    1. package com.beginnersbook.string;
    2. import java.io.StringWriter;
    3. public class WriterToString {
    4. public static void main(String args[]){
    5. // create a new writer
    6. StringWriter sw = new StringWriter();
    7. // append a char
    8. sw.append("abc");
    9. sw.append(" xyz");
    10. String str = sw.toString();
    11. System.out.println(str);
    12. }
    13. }

    输出:

    1. abc xyz