BufferedWriter class


                  see  Writer class  for descriptions of BufferedReader's methods.  They are:


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

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

.write( int c )                             Writes a single char, supplied as the low-order two bytes of a four-byte int.

.write( char[ ] c )                       Writes an array of characters.

.write( char[ ] c, int off, int len )  Writes a portion len of a character array, beginning at offset

.write( String s )                        Writes a String.

.write( String s, int off, int len )  Writes a portion len of a String, beginning at off




.newLine( )    below


  BufferedWriter adds buffering to a character output stream through chaining. Writers are not buffered by themselves.


  BufferedWriter is chained to another stream to which it sends its buffered output. Below it sends buffered characters to a PrintWriter. 


PrintWriter pw = new PrintWriter( new BufferedWriter( new FileWriter( " someoutputfile" )));


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


FileWriter fw = new FileWriter( "someoutputfile" );

BufferedWriter bw = new BufferedWriter( fw );

PrintWriter pw = new PrintWriter( bw );



  BufferedWriter adds one method of its own to those provided by Writer.  It is:


 void  .newLine( )   method


This method simply writes a system line separator to the output stream.



  Copying files.  The following snippet adds a BufferedWriter to a PrintWriter to either copy or print a file with the addition of buffering.  Selecting copying or printing depends upon which PrintWriter pw = new constructor statement you comment out below.  Right now the constructor which enables printing is commented out.


import java.io.*;


try {

BufferedReader br = new BufferedReader( new FileReader( new File( "inputfilename" )));

PrintWriter pw = new PrintWriter( new BufferedWriter( new FileWriter( new File( "outputfilename"))), true );

// PrintWriter pw = new PrintWriter( System.out, true);

String s;

    while ( ( s = br.readLine( )) != null ) {

           pw.println( s );


br.close( );

pw.close( );


catch (IOException e) {  }


  Printing a file to the console.  The program below reads a file whose name is specified as a console command line parameter.  It prints the file using buffering.


import java.io.*;


public class PrintFile{

    public static void main( String[ ] args ) {

        if ( args.length < 1) {

            System.out.println( "Error: No filename specified" );



        try {

            FileReader fr = new FileReader( args[0] );

            BufferedWriter bw = new BufferedWriter( new OutputStreamWriter( System.out ));

            int c = fr.read( );

            while ( c != -1 ) {

                bw.write( c );

                c = fr.read( );


            fr.close( );

            bw.close( );

        } catch ( IOException e ) {

            System.out.println( " I/O Error on file. " + e );