StringWriter writer = new StringWriter(); writer.write("Hello, World!"); String result = writer.toString(); System.out.println(result); // prints "Hello, World!"
StringWriter writer = new StringWriter(); writer.append("Hello"); writer.append(", World!"); String result = writer.toString(); System.out.println(result); // prints "Hello, World!"In this example, we create a new instance of a StringWriter and use its `append()` method to write the strings "Hello" and ", World!" to its in-memory buffer. The `toString()` method is again used to retrieve the buffer as a String which is then printed to the console. The StringWriter class is located in the "java.io" package library, which is used to handle input/output operations in Java.