Home Java Java-conversion Convert Inputstream to ByteArrayInputStream



Convert Inputstream to ByteArrayInputStream
Posted on: June 21, 2007 at 12:00 AM
In this example we are going to convert InputStream to ByteArrayInputStream.

Convert Inputstream to ByteArrayInputStream

     

In this example we are going to convert InputStream to ByteArrayInputStream. 

To do so first read the file as input stream  using  FileInputStream. Then convert this input stream into string and then string into byte by using the toString() and getBytes() methods respectively. Pass the reference of the byte array into a ByteArrayInputStream class object. This makes the conversion of the byte array into the ByteArrayInputStream. Finally we are printing this ByteArrayInputStream.


The code of the program is given below:

import java.io.*;
public class InputStreamToByteArrayInputStream
{
  public static void main(String args[])
  {
  try
  {
  InputStream inputStream= new FileInputStream
(
"InputStreamToByteArrayInputStream.java");
  byte currentXMLBytes[] = inputStream.toString().getBytes();
  ByteArrayInputStream byteArrayInputStream = new 
ByteArrayInputStream
(currentXMLBytes);
  while(byteArrayInputStream.read()!=-1)
  System.out.println(byteArrayInputStream.read());
  }
  catch (IOException e){}
  }
}

The output of the program is given below:

C:\rajesh\io>javac InputStreamToByteArrayInputStream.java

C:\rajesh\io>java InputStreamToByteArrayInputStream
97
97
105
46
105
101
110
117
83
114
97
64
101
53
53.......

Download this example.

Related Tags for Convert Inputstream to ByteArrayInputStream:
cstringfileinputconvertmethodsmethodgetstreamreadusingbyteintthistostringinputstreamtobyteseilputpefirstinasmnttrososadesspecinputsmeintofileinputstreamdosrespectspsoespisiriveaandstrrttstreamssriringthstfileinputndodsononp


More Tutorials from this section

Ask Questions?    Discuss: Convert Inputstream to ByteArrayInputStream   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.