This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
Hi freinds, ByteArrayOutputStream uses a byte array as the destination(as given by Api doc)Here is an example from a book 1.ByteArrayOutputStream f=new ByteArrayOutputStream(); 2.String s="This should end up in in the array"; 3.byte [] buf=s.getBytes(); 4.f.write(buf); Line4 writes the content of 'buf ' into ByteArrayOutputStream am I right here ?? If right then it seems ByteArrayOutputStream is using a byte array as source rather than a destination. I'm very confused with this please help me.
Sunil Mehta
Greenhorn
Joined: Nov 07, 2000
Posts: 8
posted
0
Hi Nasir Here is the code you are looking for. import java.io.*; public class BytArr{ public static void main(String args[]){ ByteArrayOutputStream f=new ByteArrayOutputStream(); String s="This should end up in the array"; int n = s.length(); byte bt[] = new byte[n]; for (int i=0;i<s.length();i++)> { bt[i] = (new Integer(s.charAt(i))).byteValue(); } f.write(bt,0,s.length()); System.out.println(f.toString()); } } good luck.
Sunil Mehta
Greenhorn
Joined: Nov 07, 2000
Posts: 8
posted
0
correction: line 9: for (int i=0;i<s.length();i++)>
Nasir Khan
Ranch Hand
Joined: Nov 04, 2000
Posts: 135
posted
0
Thanks Sunil but i'm not looking for the code. could anyone please help me on whatever i mentioned above.
Jane Griscti
Ranch Hand
Joined: Aug 30, 2000
Posts: 3141
posted
0
Hi Nasir, It does both . The Class libraries states: "You can use a byte array output stream to treat a <code>byte</code> array as an output stream on which you can perform write operations."
The <code>write()</code> method takes a byte array as an argument. So, it uses a byte array as an 'input' and also 'outputs' a byte array. Because it is output as a byte array, it can be read back into a byte array using ByteArrayInputStream. Hope that helps. ------------------ Jane The cure for boredom is curiosity. There is no cure for curiosity. -- Dorothy Parker [This message has been edited by Jane Griscti (edited December 10, 2000).]