DataOutputStream class


see  DataOutput interface  and  OutputStream  for descriptions and examples of DataOutputstream's  methods



DataOutputStream has these methods because it implements DataOutput:



Plus it has these methods because it extends OutputStream:

write( int ) - Writes the eight low-order bits of an int.

write( byte[ ] ) - Writes all the bytes in a byte array.

write( byte[ ], offset, len ) - Writes len  from a byte array starting at offset.

writeBoolean( boolean ) - Writes a one-byte boolean value.

writeByte( int ) - Acts the same as write( int b ) above

writeBytes( String ) - Writes low order bytes of all the chars in a String.

writeChar( int ) - Writes a two-byte char value from the low end of an int.

writeChars( String ) - Writes two char bytes of all the chars in a String.

writeDouble( double ) - Writes an eight-byte double value.

writeFloat( float ) - Writes a four-byte float value.

writeInt( int ) - Writes a four-byte int value.

writeLong( long ) - Writes an eight-byte long value.

writeShort( int ) - Writes two byte short value from low end of an int.

writeUTF( String ) - Writes a String in special modified UTF format

write( int b )  - Writes a single byte, supplied as the low-order byte in an int.

write( byte[ ] )   - Writes an array of bytes.

write( byte[ ], offset, len )   - Writes portion len of a byte array, from offset

close( )  - Closes the stream, flushing it first.

flush( )  - Flushes all associated buffers in a stream without closing it.




.size( )  -  below



  DataOutputStream provides the main implementation of the methods defined in the DataOutput interface. 


  DataOutputStream is most useful for writing Java data types to output streams so that they are portable across machine types to another Java environment.  They can then be read back with a corresponding DataOutputStream method. DataOutputStream is also useful for creating non-human-readable byte files of data fields such as numbers.  Writing number fields into records as Java primitive types is much more efficient than converting them each time and writing them as readable characters.


  DataOutputStream extends FilterOutputStream and also implements DataOutput. So it gets methods from both. It adds two methods, described at the bottom of this section.


  DataOutputStream is meant to be chained to another OutputStream which accepts its output.  It is most commonly chained to either a FileOutputStream, which writes directly to a file, or to a BufferedOutputStream to achieve buffering, and then on to a FileOutputStream. i.e.


DataOutputStream dos = new DataOutputStream(new FileOutputStream( "output_filename" ) );
DataOutputStream dos = new DataOutputStream( new BufferedOutputStream( new FileOutputStream( "output_filename" )));


  The following snippet provides a method called writeByteRcd(...) which will use four different DataOutputStream methods to write records of mixed field types to a byte data stream. It accepts four fields plus a DataOutputStream object designating the actual file to be written. The four parameters and the fixed-length record fields which they describe are as follows:


Name                String                10

ID                     String                5

Balance            Double              8

Visits                Short                2


If successful, the method returns a one.  If an IOException occurs, it returns a minus one. See  DataInputStream  for the corresponding sample method called readByteRcds(...) which can read and print this file.




     public int writeByteRcd( String name10, String id5, double balance, short visits, DataOutputStream dos ) {

         try {

             dos.writeBytes( name10 );

             dos.writeBytes( id5 );

             dos.writeDouble( balance );

             dos.writeShort( visits );

             return 1;

         } catch (IOException e) {

             return -1;




  .size( )  is added by DataOutputStream:


int  .size( ) method


  This method returns the number of bytes written to the DataOutputStream so far.


  This value cannot be reset except by closing the stream.


   The snippet below writes out various values, tells their size, and then reads them back and prints them.

try {
  File file = new File("C:\\testdos.txt");
  FileOutputStream outFile = new FileOutputStream(file);
  DataOutputStream outStream = new DataOutputStream(outFile);
  System.out.println(outStream.size ( )+" bytes were written");
  outStream.close ( );
  outFile.close ( );
  FileInputStream inFile = new FileInputStream(file);
  DataInputStream inStream = new DataInputStream(inFile);
  System.out.println(inStream.readBoolean ( ));
  System.out.println(inStream.readInt ( ));
  System.out.println(inStream.readChar ( ));
  System.out.println(inStream.readDouble ( ));
  inStream.close ( );
  inFile.close ( );
  file.delete ( );
} catch (IOException e) {   }