Results 1 to 4 of 4

Thread: Confusion converting InputStream to ByteArray

  1. #1
    Join Date
    Apr 2008
    Posts
    19

    Confusion converting InputStream to ByteArray

    Hi,

    I am a little bit confused with the conversion of InputStream to ByteArray.

    Can you guys please help me out with this confusion with some example?

    Thanks in advance.

    Regards,
    Lineman

  2. #2
    Join Date
    Aug 2008
    Posts
    71

    Re: Confusion converting InputStream to ByteArray

    Hey Sorry to say but I have a question for you.

    Why do you want to 'convert' an ObjectInputStream into a ByteArray ?

  3. #3
    Join Date
    May 2008
    Posts
    32

    Re: Confusion converting InputStream to ByteArray

    Object to byte array.

    First create a ByteArrayOutputStream and wrap it in an ObjectOutputStream now pass to a constructor. Here you can write your object to a ObjectOutputStream & close it. Now you can get the byte array from the ByteArrayOutputStream.

    I hope this helps you.

  4. #4
    Join Date
    May 2008
    Posts
    22

    Re: Confusion converting InputStream to ByteArray

    Convert InputStream to ByteArray

    To convert the InputStream into a ByteArray first we read a file from the system by using FileInputStream. Since class InputStream is super class of the FileInputStream so we can store file into InputStream to take the file as input stream. To convert the input stream into string we use toString() method and then we use the method getBytes() to convert the string into byte array. Finally we display the string as output.

    Code:
    import java.io.*;
    public class InputstramToByteArray
    {
    public static void main(String args[])throws IOException
      {
      InputStream itStrm=new FileInputStream(
     "InputstramToByteArray.java" );
      String str=itStrm.toString();
      byte[] b3=str.getBytes();
      for(int i=0;i<b3.length;i++)
      System.out.print(b3[i]+"\t");
      }
    }
    output

    Code:
    C:\Grundy\io>javac InputstramToByteArray.java
    
    C:\Grundy\io>java InputstramToByteArray
    106 97 118 97 46 105 111 46 70 105
    108 101 73 110 112 117 116 83 116 114
    101 97 109 64 51 101 50 53 97 53

Similar Threads

  1. Confusion with tata photon
    By parth mamgain in forum India BroadBand
    Replies: 1
    Last Post: 10-12-2011, 12:42 PM
  2. Confusion in Downloading some videos?
    By loveenglish in forum Technology & Internet
    Replies: 2
    Last Post: 21-08-2011, 10:43 PM
  3. Confusion in Android phone
    By Protected in forum Portable Devices
    Replies: 3
    Last Post: 13-02-2011, 05:19 PM
  4. Confusion regarding Post Graduation.......
    By krinku in forum Education Career and Job Discussions
    Replies: 1
    Last Post: 09-09-2010, 01:17 AM
  5. How to convert InputStream to String in java?
    By KALIDA in forum Software Development
    Replies: 4
    Last Post: 20-01-2010, 06:16 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,053,194.79508 seconds with 16 queries