Wednesday, July 3, 2024
HomeLanguagesJavaByteArrayOutputStream toString() method in Java with Examples

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:




// 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)

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments