ByteArrayOutputStream toString() method in Java with Examples
The toString() method of ByteArrayOutputStream class in Java is used in two ways:
1. The toString() method of ByteArrayOutputStream class in Java is used convert the buffer content of the ByteArrayOutputStream into the string. This method uses the default character set of the system. The length of the new obtained string may vary from the buffer size. Malformed input and unmappable character sequences are replaced with the default replacement string by this method.
Syntax:
public String toString()
Overrides: This method overrides the toString() method of Object class.
Parameters: This method does not accept any parameter.
Return value: This method returns the string obtained from the buffer content.
Exceptions: This method does not throw any exception.
Below program illustrates toString() method in ByteArrayOutputStream class in IO package:
Program:
import java.io.*;
public class GFG {
public static void main(String[] args)
throws Exception
{
ByteArrayOutputStream byteArrayOutStr
= new ByteArrayOutputStream();
byte [] buf = { 71 , 69 , 69 , 75 , 83 };
byteArrayOutStr.write(buf);
String str = byteArrayOutStr.toString();
System.out.println(str);
}
}
|
2. The toString(String charsetName) method of ByteArrayOutputStream class in Java is used convert the buffer content of the ByteArrayOutputStream into the string using specified charsetName which is passed as string to this method.
Syntax:
public String toString(String charsetName)
throws UnsupportedEncodingException
Parameters: This method accepts one parameter as String which represents the name of the charset supported.
Return value: This method returns the string obtained from the buffer content using supported charset.
Exceptions: This method throws UnsupportedEncodingException if the name of the charset is not supported.
Below programs illustrate toString(String charsetName) method in ByteArrayOutputStream class in IO package:
Program 1:
import java.io.*;
public class GFG {
public static void main(String[] args)
throws Exception
{
try {
ByteArrayOutputStream byteArrayOutStr
= new ByteArrayOutputStream();
byte [] buf = { 71 , 69 , 69 , 75 , 83 };
byteArrayOutStr.write(buf);
String str
= byteArrayOutStr
.toString( "UTF-8" );
System.out.println(str);
}
catch (Exception e) {
System.out.println(
"CharsetName not supported" );
}
}
}
|
Program 2:
import java.io.*;
public class GFG {
public static void main(String[] args)
throws Exception
{
try {
ByteArrayOutputStream byteArrayOutStr
= new ByteArrayOutputStream();
byte [] buf = { 71 , 69 , 69 , 75 , 83 };
byteArrayOutStr.write(buf);
String str
= byteArrayOutStr.toString( "XYZ" );
System.out.println(str);
}
catch (Exception e) {
System.out.println(
"CharsetName not supported" );
}
}
}
|
Output:
CharsetName not supported
References:
1. https://docs.oracle.com/javase/10/docs/api/java/io/ByteArrayOutputStream.html#toString()
2. https://docs.oracle.com/javase/10/docs/api/java/io/ByteArrayOutputStream.html#toString(java.lang.String)
Last Updated :
28 May, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...