728x90 AdSpace

Sunday, 6 October 2013

How to Convert String to InputStream


In this tutorials we are going to see how to convert String to InputStream using methods available in Java IO. BufferedReader is used to get String again from InputStream.
Create project called JavaMisc.
Create package called com.javatutorialscorner.io
Create java class called StringToInputStream under com.javatutorialscorner.io package.
StringToInputStream.java
package com.javatutorialscorner.io;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class StringToInputStream {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

String jtc = "This is Test String by Java Tutorials Corner";
InputStream is = new ByteArrayInputStream(jtc.getBytes());

BufferedReader br = new BufferedReader(new InputStreamReader(is));
String temp;
try {
while ((temp = br.readLine()) != null) {
System.out.println(temp);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

}

Now run the program see the following output in console.

This is Test String by Java Tutorials Corner
Annamalai Thangaraj

Annamalai is working as Technical Lead in Leading Telecom company with 5+ years experience in Identity and Access Management , Telecom and Networks, BigData, Java, Spring, Struts, Hibernate, AngularJS, and Enterprise Web Application Development.

Website: Java Tutorials Corner

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: How to Convert String to InputStream Rating: 5 Reviewed By: Annamalai Thangaraj