Monday 14, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
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

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • Java7 – Binary LiteralsIn this tutorials we are going to see the integral types (byte, short, int, and long ) in  java7.This tutorials is created by referring Oracle official web site.Some content is taken from there.  In Java7, the integ… Read More
  • Java 7 – Try statement with resourcesIn this tutorial we are going to see new features added to try-catch in Java 7. Resources in try statementOne of the most important feature added in java 7 is auto closing of resources like InputStream, BufferedInputStream,… Read More
  • Java StAX IntroductionIn this tutorial we are going to see the Java Streaming API for XML (StAX). The StAX is a streaming Java technology-based, event-driven, pull-parsing API for reading and writing XML documents. StAX enables you to create bidr… Read More
  • Get all the country using Java Locale ListIn this tutorials we are going to see how to get all the country and Country code using Java Locale. JavaCountyList.java package com.javatutorialscorner.util;import java.util.Locale;public class JavaCountyList { /** * @par… Read More
  • Java 7 – Try statement with multi catchIn this tutorial we are going to see java7 multi catch for similar type exceptionHistorically, catching multiple exceptions results in a catch block for each exception, with each block containing a variable with the type of t… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: How to Convert String to InputStream Rating: 5 Reviewed By: eHowToNow