ImportJava.io.BufferedInputStream;ImportJava.io.BufferedOutputStream;ImportJava.io.ByteArrayInputStream;ImportJava.io.ByteArrayOutputStream;ImportJava.io.DataInputStream;ImportJava.io.DataOutputStream;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;Importjava.io.IOException; Public classDataStream { Public Static voidMain (string[] args)throwsIOException {//Write ("Test5.txt");//Read ("Test5.txt");Readfrombuffer (Writetobuffer ()); } //write to local file Public Static voidWrite (String path)throwsioexception{DataOutputStream out=NewDataOutputStream (NewBufferedoutputstream (NewFileOutputStream (path)); Out.writeint (1); Out.writeshort (2); Out.writelong (1); Out.writeutf ("Some text"); Out.flush (); Out.close (); } //read from local file Public Static voidRead (String path)throwsioexception{DataInputStream in=NewDataInputStream (NewBufferedinputstream (NewFileInputStream (path)); System.out.println (In.readint ()); System.out.println (In.readshort ()); System.out.println (In.readlong ()); System.out.println (In.readutf ()); In.close (); } //write to byte array Public Static byte[] Writetobuffer ()throwsioexception{byte[] Buff =NULL; Bytearrayoutputstream Bos=NewBytearrayoutputstream (); DataOutputStream out=NewDataOutputStream (NewBufferedoutputstream (BOS)); Out.writeint (1); Out.writeshort (2); Out.writelong (1); Out.writeutf ("Some text"); Out.flush (); Out.close (); Buff=Bos.tobytearray (); Bos.close (); returnBuff; } //read from byte array Public Static voidReadfrombuffer (byte[] b)throwsioexception{DataInputStream in=NewDataInputStream (NewBufferedinputstream (NewBytearrayinputstream (b))); System.out.println (In.readint ()); System.out.println (In.readshort ()); System.out.println (In.readlong ()); System.out.println (In.readutf ()); In.close (); } }
DataInputStream and DataOutputStream