Home »
Java programming language
Java StringWriter toString() Method with Example
StringWriter Class toString() method: Here, we are going to learn about the toString() method of StringWriter Class with its syntax and example.
Submitted by Preeti Jain, on April 25, 2020
StringWriter Class toString() method
- toString() method is available in java.io package.
- toString() method is used to represent the buffer current value in terms of string.
- toString() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
- toString() method does not throw an exception at the time of returning buffer value.
Syntax:
public String toString();
Parameter(s):
- It does not accept any parameter.
Return value:
The return type of the method is String, it returns the string denotation of this StringWriter stream.
Example:
// Java program to demonstrate the example
// of String toString() method of StringWriter
import java.io.*;
public class ToStringOfFSW {
public static void main(String[] args) throws Exception {
StringWriter str_w = null;
String str = "Java World!!!";
try {
// Instantiates StringWriter
str_w = new StringWriter();
str_w.write(str);
// By using toString() method is to
// represent the stream as a string
String s = str_w.toString();
System.out.println("str_w.toString(): " + s);
} catch (Exception ex) {
System.out.println(ex.toString());
} finally {
// with the help of this block is to
// free all necessary resources linked
// with the stream
if (str_w != null) {
str_w.close();
}
}
}
}
Output
str_w.toString(): Java World!!!