Java InputStream to String

Filed Under: Java

Today we will learn how to convert InputStream to String in Java. While working on files, sometimes we have to read files and then convert InputStream data to String for further processing.

Java InputStream to String

InputStream to String

We can convert InputStream to String in many ways.

  1. BufferedReader
  2. StringWriter
  3. Scanner

Let’s look into all these classes to convert InputStream object to String.

1. BufferedReader

Here is a simple program showing java InputStream to String conversion using BufferedReader.


import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class InputStreamToStringUsingBufferedReader {

	final static String fileName = "/Users/pankaj/Downloads/file.txt";

	public static void main(String[] args) throws IOException {
		long start = System.currentTimeMillis();
		FileInputStream fileInputStream = null;
		BufferedReader bufferedReader = null;
		StringBuilder inputSB = null;
		try {
			fileInputStream = new FileInputStream(fileName);
			bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream, "UTF-8"));
			inputSB = new StringBuilder();
			String line = bufferedReader.readLine();
			while (line != null) {
				inputSB.append(line);
				line = bufferedReader.readLine();
				if (line != null) {
					// add new line character
					inputSB.append("\n");
				}
			}
		} finally {
			bufferedReader.close();
			fileInputStream.close();
		}
		String str = inputSB.toString();
		long end = System.currentTimeMillis();
		System.out.println("InputStream to String using BufferedReader = " + (end - start) + "ms");
	}

}

2. StringWriter

We can use StringWriter too for converting InputStream to String. Note that it’s also using BufferedReader but writing data to StringWriter and then easily converting to String.


import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;

public class InputStreamToStringUsingStringWriter {

	final static String fileName = "/Users/pankaj/Downloads/file.txt";

	public static void main(String args[]) throws IOException {
		long start = System.currentTimeMillis();
		FileInputStream fileInputStream = null;
		Reader reader = null;
		Writer writer = new StringWriter();
		char[] buffer = new char[1024];
		try {
			fileInputStream = new FileInputStream(fileName);
			reader = new BufferedReader(new InputStreamReader(fileInputStream, "UTF-8"));
			int n;
			while ((n = reader.read(buffer)) != -1) {
				writer.write(buffer, 0, n);
			}
		} finally {
			reader.close();
			writer.close();
			fileInputStream.close();
		}
		String str = writer.toString();
		long end = System.currentTimeMillis();
		System.out.println("InputStream to String using StringWriter = " + (end - start) + "ms");
	}
}

3. Scanner

We can also use Scanner class for reading a file as InputStream and convert to String.


import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;

public class InputStreamToStringUsingScanner {

	final static String fileName = "/Users/pankaj/Downloads/file.txt";

	public static void main(String[] args) throws IOException {
		long start = System.currentTimeMillis();
		FileInputStream fileInputStream = null;
		Scanner scanner = null;
		try {
			fileInputStream = new FileInputStream(fileName);
			scanner = new Scanner(fileInputStream, "UTF-8");
			String str = scanner.useDelimiter("\\A").next();
		} finally {
			fileInputStream.close();
			scanner.close();
		}
		long end = System.currentTimeMillis();
		System.out.println("InputStream to String using Scanner = "+(end-start)+"ms");
	}

}

Java InputStream to String Test

I executed all the above programs on a file of 176 MB. Below is the result of the test run.

Java InputStream to String, convert inputstream to string in java

That’s all for converting InputStream to String in Java. If you are looking for the best performance, then you should do some test run with sample data of your choice and then go for the best performing method.

References:

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages