Java Program to Convert InputStream to String

Java Program to Convert InputStream to String

What are Strings?

A string is a sequence of characters. In Java, strings are used to represent text. You can use strings to store messages, names, and other kinds of text data.
Strings are usually represented as a series of characters enclosed in quotation marks. For example, in Java, you can create a string like this:
String s = “Hello World”;

In this Article, we will write a program to Convert Character to String and Vice-Versa.

Strings:

We can use the various methods of the String class to manipulate strings.
For example, you can use the length() method to get the length of a string, the substring() method to get a substring of a string, and the toUpperCase() method to convert a string to uppercase. You can also use the + operator or the concat() method to concatenate two strings.

What are InputStreams?

In Java, an InputStream is a base class for representing input streams of bytes. Subclasses of InputStream provide actual implementations for reading bytes from a variety of sources such as a file, network connection, or in-memory buffer. InputStreams are typically used to read data from a source and then pass that data to another part of the program for further processing. Examples of classes that extend InputStream include FileInputStream, ByteArrayInputStream, and BufferedInputStream.

InputStream to String :

In Java, you can convert an InputStream to a String using the InputStreamReader and BufferedReader classes. The InputStreamReader class can convert the bytes from an InputStream into characters, and the BufferedReader class can read these characters and buffer them for efficient reading.

Program to convert InputStream to String :

Run
import java.io.*;

public class Main{

    public static String convert(InputStream inputStream) {
        StringBuilder stringBuilder = new StringBuilder();
        
        // Reading file of inputstream file
        try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream))) {
            String line;
            
            // Reading line of the file
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }

    public static void main(String[] args) {
        // Initializing Inputstream file
        InputStream inputStream = new ByteArrayInputStream("Hello there!".getBytes());
        
        // Calling the convert function
        String result = convert(inputStream);
        System.out.println(result);
    }
}
Output:

Hello there!

In this example, the convert method takes an InputStream as its parameter and returns the corresponding String. The method reads the bytes from the InputStream using a BufferedReader, and appends each line to a StringBuilder. The resulting String is returned.

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