Dear all
I am having a problem to save from ByteArrayInputStream to file..
Refer to the following excerpt of code:
...............
DatagramPacket receivePacket = new
DatagramPacket (buffer, buffer.length);
socket.receive(receivePacket);
System.out.println("Packet received");
System.out.println("\nFrom host" + receivePacket.getAddress() +
"\nHost port" + receivePacket.getPort() +
"\nLength" + receivePacket.getLength()); // +
// "Word" + s);
ByteArrayInputStream bais = new
ByteArrayInputStream(receivePacket.getData(),
0,receivePacket.getLength());
The question is how can I save the bais (ByteArrayInputStream) as a
binary file ?
Any help is really appreciated. Thanks in advanced.
Gordon Beaton - 13 Jun 2007 07:45 GMT
> ByteArrayInputStream bais = new ByteArrayInputStream(receivePacket.getData(),
> 0,receivePacket.getLength());
>
> The question is how can I save the bais (ByteArrayInputStream) as a
> binary file ?
If you just want to save the datagram contents, there's no need for
the ByteArrayInputStream here at all.
Use DatagramPacket.getData() to get the data from the packet as a byte
array. Use a FileOutputStream to write the contents of the byte array
to a file.
/gordon
--
Roedy Green - 15 Jun 2007 10:56 GMT
>The question is how can I save the bais (ByteArrayInputStream) as a
>binary file ?
The whole point of a ByteArrayInputStream is to get you a byte[].
Now your problem is how to write that byte[] to a file.
The file I/O Amanuensis will show you dozens of ways to do that.
see http://mindprod.com/applets/fileio.html
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com