Home > Java > javaTutorial > What is the importance of StringWriter in Java?

What is the importance of StringWriter in Java?

PHPz
Release: 2023-09-10 16:57:03
forward
1360 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

<strong>public class StringWriter extends Writer</strong>
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!

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
Latest Issues
Install JAVA
From 1970-01-01 08:00:00
0
0
0
Unable to install java
From 1970-01-01 08:00:00
0
0
0
Can java be used as the backend of the web?
From 1970-01-01 08:00:00
0
0
0
Is this in Java language?
From 1970-01-01 08:00:00
0
0
0
Help: JAVA encrypted data PHP decryption
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template