OutputStream class

 

See the following byte classes which have OutputStream's methods:  ByteArrayOutputStream class,  BufferedOutputStream class, 

DataOutPutStream class,  FileOutputStream class,  FilterOutputStream class,  ObjectOutputStream class, and 

PipedOutputStream class.

 

 

  OutputStream is the abstract class which defines the five basic methods for all of the byte stream output classes listed above. You can use these method forms with all the subclasses listed above.  You can write to a file or to an OutputStream, depending on the class you use.

 

  The five basic methods are:

 

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 a portion len of a byte array, beginning at offset

close( )                                     Closes the stream, flushing it first.

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

 

 

void  write( int )   method

 

  Writes a single byte, supplied as the low-order byte in an int.   i.e. The snippet writes 1 to 5 from ints and then reads them back in as bytes and prints them.

 

import java.io.*;

 

try {

     FileOutputStream fos = new FileOutputStream( "my_output" );

     DataOutputStream dos = new DataOutputStream( fos );

     for ( int x = 1; x <= 5; x++ ) {

                dos.write( x );  }

     dos.close ( );

 

    DataInputStream dis = new DataInputStream( new BufferedInputStream( new FileInputStream( "my_output")));

    while(true) System.out.print( String.valueOf(dis.readByte( ) ));

}

catch (EOFException eof) { System.out.println(eof);  }

catch (IOException io) {  }

 

 

void  write( byte[ ] )   method

 

   Writes an array of bytes.  It writes the whole array.  i.e.

 

import java.io.*;

 

try {

     FileOutputStream fos = new FileOutputStream( "outputfilename" );

     DataOutputStream dos = new DataOutputStream( fos );

     byte[ ] ba = { 1,2,3,4,5,6,7,8,9};

     dos.write( ba );

     dos.close ( );

}

catch (IOException io) {  }

 

 

void  write( byte[ ], offset, len )    method   

 

  Writes a portion len of a byte array, beginning at offset.  The snippet is the same as above, except it just writes the 567.

 

import java.io.*;

 

try {

     FileOutputStream fos = new FileOutputStream( "outputfilename" );

     DataOutputStream dos = new DataOutputStream( fos );

     byte[ ] ba = { 1,2,3,4,5,6,7,8,9};

     dos.write( ba, 4, 3 );

     dos.close ( );

}

catch (IOException io) {  }

 

void  close( )      method                   

 

  Closes the stream, flushing it first.

 

 

void  flush( )       method                     

 

  This method flushes the data output stream, forcing any buffered data to be written out.

 

  You should always either use flush( ) or close( ) to ensure that all bytes get written. A close( ) does a flush( ).