Home > Java > Java Tutorial > body text

What is the importance of StringWriter in Java?

PHPz
Release: 2023-09-10 16:57:03
forward
1186 people have browsed it

The

What is the importance of StringWriter in Java?

StringWriter class is a subclass of the Writer class that writes strings to the output stream. To write a string, the character stream collects the string into a string buffer and then constructs a string. StringWriter's buffer grows automatically based on data. The important methods of the StringWriter class are write(), append(), getBuffer(), flush() and close(). p>

Syntax

public class StringWriter extends Writer
Copy after login

Example

import java.io.*;
public class StringWriterTest {
   public static void main(String args[]) {
      String str = "Welcome to Tutorials Point";
      try {
         StringWriter sw = new StringWriter();
         sw.write(str);
         StringBuffer sb = new StringBuffer();
         sb = sw.getBuffer();
         System.out.println("StringBuffer: "+ sb);
         System.out.println("String written by StringWriter: "+ sw);
         sw.close();
      } catch (IOException ioe) {
         System.out.println(ioe);
      }
   }
}
Copy after login

Output

StringBuffer: Welcome to Tutorials Point
String written by StringWriter: Welcome to Tutorials Point
Copy after login

The above is the detailed content of What is the importance of StringWriter in Java?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!