Tag Archives: Java IO

Java – Convert String to InputStream

If you want to convert a String into InputStream, you can use the following piece of code.

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import org.apache.commons.io.IOUtils;

public class ConvertStringToInputStream {

	public static void main(String[] args) {
		try {
			// Convert the String into InputStream 
			String eurekaUrl = "https://ykyuen.wordpress.com";
			InputStream is = new ByteArrayInputStream(eurekaUrl.getBytes("UTF-8"));
			// Use Apache Commons IO to get the content of the InputStream
			System.out.println(IOUtils.toString(is, "UTF-8")); 
		} catch (Exception e) {


Continue reading Java – Convert String to InputStream

Java – Convert InputStream to String

Recently, i have created a simple Java Servlet server which could parse a request xml from the HTTP POST Request and then return the HTTP Response together with the response xml. In the server implementation, i need to convert an InputStream into a String. The following code shows you how to make it.

In this example, the program will read the data.txt file and print the content to the console. Continue reading Java – Convert InputStream to String