Java Program to Convert OutputStream into String

Java Program to Convert OutputStream to String

What is OutputStream in Java ?

In Java, an OutputStream is an abstract class that is used to represent an output stream of bytes. It is part of the Java I/O (Input/Output) package (java.io). OutputStream provides the basic methods to write bytes to an output destination, such as a file, network socket, or in-memory buffer.

OutputStream defines several methods for writing bytes, including write(int b), which writes a single byte, and write(byte[] b), which writes an array of bytes.

What is List in Java ?

In Java, a String is a sequence of characters that represent text. It is an object in the Java programming language, and is commonly used to store and manipulate text in a program. Strings can be created using double quotes, and can be concatenated using the + operator. They are also immutable, meaning that once a String object is created, its value cannot be changed.

OutputStreams :

A common use of OutputStreams is to write data to a file. The FileOutputStream class is a concrete implementation of OutputStream that writes bytes to a file. For example, the following code writes the string “Hello, world!” to a file called “output.txt”:

    String message = "Hello, world!";
    byte[] data = message.getBytes();
    OutputStream output = new FileOutputStream("output.txt");
    output.write(data);
    output.close();

Other classes that extend OutputStream include BufferedOutputStream, which improves the performance of writing to a file by buffering the output, and PrintStream, which provides methods for printing formatted output.

Example 1 :

Run

import java.io.ByteArrayOutputStream;

public class Main {
    public static void main(String[] args) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        String str = "Prepinsta";
        baos.write(str.getBytes(), 0, str.length());
        System.out.println(baos.toString());
    }
}


Output :

Prepinsta

Explanation:

In this code, we first create an object of ByteArrayOutputStream by calling its constructor new ByteArrayOutputStream(). Then, we create a String variable str and initialize it with the value “Hello World!”. We use the write() method of the ByteArrayOutputStream class to copy the contents of the string to the object of ByteArrayOutputStream.

We pass three arguments to the write() method:

  • The bytes of the string to be written
  • The offset in the byte array to start writing from
  • The number of bytes to write
    Finally, We print the contents of the ByteArrayOutputStream object by calling the toString() method which gives the same string we have written to the OutputStream.

Example 2 :

Run
import java.io.ByteArrayOutputStream;

public class Main {
    public static void main(String[] args) {
        byte[] byteArray = new byte[]{104, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100};
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        baos.write(byteArray, 0, byteArray.length);
        System.out.println(baos.toString());
    }
}

Output :

hello World

Explanation:

In this code, we first create a byte array byteArray and store the ASCII values of the characters in it. Then, we create an object of ByteArrayOutputStream by calling its constructor new ByteArrayOutputStream(). We use the write() method of the ByteArrayOutputStream class to copy the contents of the byte array to the object.

We pass three arguments to the write() method:

  • The byte array to be written
  • The offset in the byte array to start writing from
  • The number of bytes to write
    Finally, We print the contents of the ByteArrayOutputStream object by calling the toString() method which gives the same string we have written to the OutputStream.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription