StringWriter sw = new StringWriter(); sw.write("This is a sample text."); String str = sw.toString(); System.out.println(str); sw.close();
StringWriter sw = new StringWriter(); sw.append("This is the first sentence."); sw.append("This is the second sentence."); String str = sw.toString(); System.out.println(str); sw.close();In this example, we create a StringWriter object 'sw', append two sentences to it using the 'append' method, convert it to a String, and print the resulting string. Finally, we close the StringWriter using the 'close' method. Package Library: java.io