BufferedReader class

 

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

 

.mark( readAheadLimit )  Mark the present position in the stream.

.markSupported( ) Tell whether this stream supports the mark(...) operation.

.read( )  Read a single character, returning it in the low end two bytes of an int.

.read( char[ ] )  Read characters into a char array.

.read( char[ ], offset, len)  Read characters into a portion of a char array.

.ready( ) Tell whether this stream is ready to be read.

.reset( )  Reset the stream.

.skip( n )  Skip over n characters in the stream.

.close( ) Close the stream.

 

plus

 

.readLine( )   below

 

 

  BufferedReader is a Reader subclass which buffers incoming characters and provides a readline( ) method for obtaining whole lines.  (The default buffer is large and usually adequate so changing its size is not dealt with here.)

 

  BufferedReader is the preferred method for reading lines in character text files, since the only other readline( ) method for reading characters- in DataInputStream - has been deprecated.

 

  BufferedReader's constructors will take any Reader stream object. A BufferedReader is usually constructed from a FileReader or from an InputStreamReader.  i.e.

 

File f = new File( "somecharfile" )

FileReader fr = new FileReader( f );                       

BufferedReader br = new BufferedReader(  fr );   

or

File f = new File( "somebytefile" )

FileInputStream fis = new FileInputStream(  f );

InputStreamReader isr = new InputStreamReader( fis );                   

 

  BufferedReader supports mark(...) and reset( ).

 

 

String  .readLine( )  method

 

  BufferedReader adds this line-reading method to those of Reader.

 

  readline( ) reads a line up to a \r carriage return, a \n line feed, or to a \r\n combination, and then stops. Note that these characters are not returned.

 

  Unlike the other read( ) methods provided under Reader, readLine( ) returns a null to signal end of stream.  It does not return a minus one.

 

 

  This snippet chains a FileReader stream into a BufferedReader to read with buffering.  It reads and then prints lines from a character text file.  If the file happens to contain UTF-16 16-bit characters, the printing will appear distorted.

 

import java.io.*;

 

try {

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

    String line;           

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

        System.out.println( line );

    }                       

    br.close( );

} catch (Exception ex) {  } 

 

 

 

  The following snippet uses a BufferedReader, chained to an InputStreamReader, to read from the standard input console.  The snippet simply prints back any line which is entered.  For an explanation of how the source byte stream from System.in is converted to characters here, see the InputStreamReader class.

 

import java.io.*;

 

BufferedReader br = new BufferedReader( new InputStreamReader(System.in));     

System.out.println( "Enter a line now:" );

System.out.println( br.readLine( ) );

 

 

  The following BufferedReader snippet reads and counts characters in a character file using buffered reading.  It adds up the characters a "line's worth" at a time rather than singly.  This is a buffered alternative to the unbuffered character-counting example which can be found under FileReader class.

 

import java.io.*;

 

BufferedReader br = new BufferedReader(new FileReader( "mycharfile" ));

int x = 0;

String s;

while (true) {

        s = br.readLine( );

        if ( s == null ) {

            break;

        }

        x += s.length( );

        }      

System.out.println( "The file contains " + x + "characters" );