Skip to content
Related Articles

Related Articles

Improve Article
ByteArrayOutputStream toString() method in Java with Examples
  • Last Updated : 28 May, 2020

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)

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :