Open In App

ByteArrayOutputStream toString() method in Java with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

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:




// Java program to illustrate
// ByteArrayOutputStream toString() method
  
import java.io.*;
  
public class GFG {
    public static void main(String[] args)
        throws Exception
    {
  
        // Create byteArrayOutputStream
        ByteArrayOutputStream byteArrayOutStr
            = new ByteArrayOutputStream();
  
        // Create byte array
        byte[] buf = { 71, 69, 69, 75, 83 };
  
        // Write byte array
        // to byteArrayOutputStream
        byteArrayOutStr.write(buf);
  
        // Revoke toString() method
        String str = byteArrayOutStr.toString();
  
        // Print the string
        System.out.println(str);
    }
}


Output:

GEEKS

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:




// Java program to illustrate
// ByteArrayOutputStream
// toString(String charsetName) method
  
import java.io.*;
  
public class GFG {
    public static void main(String[] args)
        throws Exception
    {
        try {
            // Create byteArrayOutputStream
            ByteArrayOutputStream byteArrayOutStr
                = new ByteArrayOutputStream();
  
            // Create byte array
            byte[] buf = { 71, 69, 69, 75, 83 };
  
            // Write byte array
            // to byteArrayOutputStream
            byteArrayOutStr.write(buf);
  
            // Revoke toString(String) method
            String str
                = byteArrayOutStr
                      .toString("UTF-8");
  
            // Print the string
            System.out.println(str);
        }
        catch (Exception e) {
            System.out.println(
                "CharsetName not supported");
        }
    }
}


Output:

GEEKS

Program 2:




// Java program to illustrate
// ByteArrayOutputStream
// toString(String charsetName) method
  
import java.io.*;
  
public class GFG {
    public static void main(String[] args)
        throws Exception
    {
        try {
            // Create byteArrayOutputStream
            ByteArrayOutputStream byteArrayOutStr
                = new ByteArrayOutputStream();
  
            // Create byte array
            byte[] buf = { 71, 69, 69, 75, 83 };
  
            // Write byte array
            // to byteArrayOutputStream
            byteArrayOutStr.write(buf);
  
            // Revoke toString(String charsetName)
            // method
            String str
                = byteArrayOutStr.toString("XYZ");
  
            // Print the string
            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
Previous
Next
Share your thoughts in the comments
Similar Reads