Example program:
Packageio;ImportJava.io.ByteArrayInputStream;ImportJava.io.ByteArrayOutputStream;ImportJava.io.DataInputStream;ImportJava.io.DataOutputStream;Importjava.io.IOException; Public classTestdatastream { Public Static voidMain (string[] args) {/*** Create a byte array in memory, Bytearrayoutputstream this pipe is godless to the memory*/Bytearrayoutputstream BAOs=NewBytearrayoutputstream (); //writes the basic Java data type to the output streamDataOutputStream dos =NewDataOutputStream (BAOs); Try { //writes a double type, 8 bytesdos.writedouble (Math.random ()); Dos.writeboolean (true);//Boolean type 1 bytes /*** Equivalent to this pipe godless to the top of Bytearrayoutputstream. Got a reference to the byte array in memory. */Bytearrayinputstream Bais=NewBytearrayinputstream (Baos.tobytearray ()); //The number of readable bytes remaining in the input streamSystem.out.println (Bais.available ());//altogether 9 bytesdatainputstream Dis=NewDataInputStream (Bais); System.out.println (Dis.readdouble ()); System.out.println (Dis.readboolean ()); /*** After Colse, the memory byte array is gone. */Dos.close (); Dis.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } }}
Java IO stream data flow datainputstream, DataOutputStream, Bytearrayinputstream, Bytearrayoutputstream