BufferedOutputStream class

 

                  see OutputStream class for descriptions of methods BufferedOutputStream can use.  They 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.

 

 

  BufferedOutputStream adds buffering to byte output stream operations.

 

  BufferedOutputStream is chained to another stream to which it sends its buffered output. Below it sends buffered characters to a PrintStream. 

 

PrintStream ps = new PrintStream( new BufferedOutputStream( new FileOutputStream( " output_filename" )));

 

  The statement above is the model for how you can insert a BufferedWriter between the stream providing the character sink (on the right) and the source stream (on the left). It is also the same as doing it this way, with three statements:

 

FileOutputStream fos = new FileOutputStream ( "output_filename" );

BufferedOutputStream bos = new BufferedOutputStream ( fos );

PrintStream ps = new PrintStream ( bos );

 

  BufferedOutputStream adds one method to those provided by Writer.  It is void flush( ), which forces all buffered output bytes to be written out to the underlying output stream.  Since close( ) also does a flush, you should always use close( ) or flush( ) to ensure all your data actually gets written.

 

  The following snippet adds a BufferedOutputStream to a FileOutputStream to copy a file using buffered write operations from BufferedOutputStream.  It also reads the file to be copied with buffering by adding a BufferedInputStream to a FileInputStream.

 

import java.io.*;

 

try {

    BufferedInputStream bis = new BufferedInputStream( new FileInputStream(new File( "c:\\FileASCII.txt" )));

    BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream( "c:\\output_filename" ));

    int x; 

    while ((x = bis.read( )) != -1) { 

         bos.write( x );

    }

    bis.close( );

    bos.close( );

} catch (IOException e) {  }

 

  The program below uses BufferedOutputStream buffering to write a series of 4-byte ints to a file.  In three mandatory command line parameters you must specify, respectively, the output file name, the desired size of the buffer, and the desired number of ints to be written.  The ints always start at 1.  In order to use the writeInt(..) method, which is unavailable in BufferedOutputStream, the BufferedOutputStream is wrapped by a DataOutputStream. This program also illustrates several types of validation checks on the incoming console parameter values.

 

import java.io.*;

 

public class WriteInts {

public static void main( String[ ] args) {

 int x = 0;

 int y = 0;

 FileOutputStream fos;

 BufferedOutputStream bos;

 if ( args.length < 3 ) {                                           // check for an insufficient number of parameter

     System.out.println(

      "Try again. You must enter 3 parameters: output filename, buffer size, and record limit number." );

     System.exit( 0 );

 }

 try {  x = Integer.parseInt( args[1] );  }

 catch (NumberFormatException e) { // check for an invalid numeric

     System.out.println( e + "\nBad buffer size number entered. Try again." );

     System.exit(0);

 }

 try {  y = Integer.parseInt(args[ 2 ] );  }             // check for an invalid numeric

 catch (NumberFormatException e) {

     System.out.println( e + "\nBad record limit number entered. Try again." );

     System.exit( 0 );

 }

 if ( x < 1 || y < 1 ) {                                 // check for zero or negative numerics

     System.out.println( "Both buffer and record control must be greater than zero. Try again." );

     System.exit( 0 );

 }

  try {

     fos = new FileOutputStream( args[0] );

     bos = new BufferedOutputStream( fos, x );

     DataOutputStream dos = new DataOutputStream( bos );

     for  ( int i = 1; i <= y; i++ ) {  dos.writeInt( i );  }

     dos.close( );

 }

 catch (IOException e) {                       // catch an invalid filename parameter

     System.out.println( e + "\nBad filename entered. Try again." );

     System.exit( 0 );

 }

}

}