Results 1 to 9 of 9
  1. #1
    nitishjtm is offline Member
    Join Date
    Sep 2010
    Posts
    30
    Rep Power
    0

    Post Using ByteArrayInputStream

    Hi I have written following program testing byte array input stream. Using first constructor, it gives correct output. But in second constructor, instead of printing the output once it prints the same output for 200 times i.e. the size of byte array. Please tell me what to do.
    The code is as follows:
    package com.nitish.io.input;
    import java.io.*;

    public class ByteArrayInputStreamEx{
    public static void main(String []s)
    {
    String str = "Nitish";
    byte b[] = str.getBytes();
    ByteArrayInputStream bais = new ByteArrayInputStream(b);
    int i = bais.read();
    //System.out.println((char)i);
    while(i!=-1){
    System.out.println(i);
    i=bais.read();
    }//while
    System.out.println("*****Using Second Constructor*****");
    ByteArrayInputStream bais1 = new ByteArrayInputStream(b,0,b.length);
    byte b1[] = new byte[200];
    int j = bais1.read(b1,0,b1.length);
    for(byte b2:b1){
    String str1 = new String(b1,0,b1.length);
    System.out.println(str1);
    }
    }//main
    }//class

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    for(byte b2:b1)
    How many times will this loop?

    Please tell me what to do.
    You have coded the program to do what it does.
    What do you want it to do differently from how you have coded it?

  3. #3
    nitishjtm is offline Member
    Join Date
    Sep 2010
    Posts
    30
    Rep Power
    0

    Post Using ByteArrayInputStream

    Sir, please tell me the necessary modifications

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    You have not answered my questions.
    tell me the necessary modifications
    To do what?

    Explain to me what you want this code do to.
    Have you tried changing it so it will do what you want?
    Java Code:
    for(byte b2:b1){
      String str1 = new String(b1,0,b1.length); 
      System.out.println(str1);
    }

  5. #5
    nitishjtm is offline Member
    Join Date
    Sep 2010
    Posts
    30
    Rep Power
    0

    Post Using ByteArrayInputStream

    Sir, I realized my mistake.
    Using second constructor I want to read bytes into the byte array that I have declared, and from that byte array I want to print each byte. The problem that I am getting is that it prints output "Nitish" for 200 times but I want the same output as I am getting using first constructor, i.e.
    N
    i
    t
    i
    s
    h
    Last edited by nitishjtm; 10-03-2010 at 08:41 PM.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    from that byte array I want to print each byte.
    In what format do you want to print the bytes? int, hex, char, String?

  7. #7
    nitishjtm is offline Member
    Join Date
    Sep 2010
    Posts
    30
    Rep Power
    0

    Default

    I want to print those bytes in char format

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    Have you tried casting:
    (char)b2

  9. #9
    nitishjtm is offline Member
    Join Date
    Sep 2010
    Posts
    30
    Rep Power
    0

Similar Threads

  1. ByteArrayInputStream and ByteArrayOutputStream
    By new_2_java in forum New To Java
    Replies: 6
    Last Post: 07-31-2008, 10:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •