Spread the love

Class BufferedReader in Java

Spread the love

Class BufferedReader in Java

A BufferedReader object provides a more efficient way to read just a few characters at a time from a Reader. BufferedReader objects use a buffer to store input from an associated Reader. In other words, a large number of characters are read from the underlying reader and stored in an internal buffer.

A BufferedReader is more efficient than a regular Reader because reading data from memory is faster than reading it from a disk or a network. All reading is done directly from the buffer; the disk or network needs to be accessed only occasionally to fill up the buffer.

We should wrap a BufferedReader around any Reader whose read() operations may be time
consuming or costly, such as a FileReader or InputStreamReader.

BufferedReader provides a way to mark a position in the stream and subsequently reset the stream to that position, using mark() and reset().

A BufferedReader is similar to a BufferedInputStream, but it operates on a stream of Java
characters instead of a byte stream, which makes it easier to support internationalization.

The structure of the class BufferedReader is given by

public class java.io.BufferedReader extends java.io.Reader {
// Constructors
public BufferedReader(Reader in);
public BufferedReader(Reader in, int sz);
// Instance Methods
public void close();
public void mark(int readAheadLimit);
public boolean markSupported();
public int read();
public int read(char[] cbuf, int off, int len);
public String readLine();
public boolean ready();
public void reset();
public long skip(long n);
}

The details of the class structure are given as follows:

public BufferedReader(Reader in);

public BufferedReader(Reader in) constructor creates a BufferedReader that buffers input from the given Reader using a buffer with the default size of 8192 characters.

READ  Interface Concept Simplified In Java

Parameter
in – The reader to buffer.

public BufferedReader(Reader in, int sz);

public BufferedReader(Reader in, int sz) constructor creates a BufferedReader that buffers input from the given Reader, using a buffer of the given size.

Parameter
in – The reader to buffer.
sz – The size of buffer to use.

public void close();

public void close() method closes this BufferedReader and its underlying Reader.

public void mark(int readAheadLimit);

public void mark(int readAheadLimit) method causes the BufferedReader to remember its current position. A subsequent call to reset() causes the object to return to that saved position, and thus reread a portion of the buffer.

READ  Variables in Java

Parameter
readlimit -The maximum number of bytes that can be read before the saved position becomes invalid.

public boolean markSupported();

public boolean markSupported() method returns true to indicate that this class supports mark() and reset().
This method returns the boolean value true.

public int read();

public int read() method returns the next character from the buffer. If all the characters in the buffer have been read, the buffer is filled from the underlying Reader, and the next character is returned. If the buffer does not need to be filled, this method returns immediately.

If the buffer needs to be filled, this method blocks until data is available from the underlying Reader, the end of the stream is reached, or an exception is thrown.

This method returns the next character of data, or -1 if the end of the stream is encountered.

public int read(char[] cbuf, int off, int len);

public int read(char[] cbuf, int off, int len) method reads characters from the internal buffer into the given array cbuf, starting at index off and continuing for up to len bytes. If there are any characters in the buffer, this method returns immediately. Otherwise, the buffer needs to be filled; this method blocks until the data is available from the underlying InputStream, the end of the stream is reached, or an exception is thrown.

READ  Class NotSerializableException in Java

This method returns the actual number of characters read or -1 if the end of the stream is encountered immediately.

Parameter
cbuf – An array of characters to be filled from the stream.
off – Offset into the character array.
len – Number of characters to read.

public String readLine();

public String readLine() method reads a line of text. Lines are terminated by “\n”, “\r”, or “\r\n”. The line terminators are not returned with the line string.

READ  Class Hashtable in Java

This method returns a String containing the line just read, or null if the end of the stream has been reached.

public boolean ready();

public boolean ready() method returns true If there is data in the buffer, or if the underlying stream is ready. The underlying stream is ready if the next read() is guaranteed to not block. Note that a return value of false does not guarantee that the next read operation will block.

This method returns a boolean value that indicates whether the stream is ready to be read.

public void reset();

public void reset() method sets the position of the BufferedReader to a position that was saved by a previous call to mark(). Subsequent characters read from this BufferedReader will begin from the saved position and continue normally.

public long skip(long n);

public long skip(long n) method skips n characters of input. If the new position of the stream is still within the data contained in the buffer, the method returns immediately. Otherwise, the buffer is repeatedly filled until the requested position is available.

This method returns the actual number of characters skipped.

READ  Class Hashtable in Java

Parameter
n – The number of characters to skip.
Apart from these BufferedWriter class also has inherited methods from class- Object. They are as follows:

  • clone()
  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • toString()
  • wait(long)

BufferedWriter class also has one inherited method from class- Reader. That is as follows:

  • read(char[])




Spread the love
READ  Class NotSerializableException in Java
Animesh Chatterjeehttps://techtravelhub.com/
I am the founder and owner of the blog - TechTravelHub.com, always love to share knowledge on test automation,tools,techniques and tips.I am a passionate coder of Java and VBScript.I also publish articles on Travel ideas and great honeymoon destinations.Apart from these, I am a gear-head,love to drive across India. I have shared lots of articles here on How to travel several parts of India.Customization of cars aka car modification is my another hobby.Get in touch with me on ani01104@gamil.com

Related Articles

Super Keyword in Java Simplified

Super Keyword Super Keyword in Java Simplified: Java defines a special keyword called...

Comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Recent Posts

Super Keyword in Java Simplified

Super Keyword Super Keyword in Java Simplified: Java defines a special...

Learn Object Repository in UFT in An Efficient and Fast Manner

Object Repository in UFT The post will cover the following...

Learn What Primary Tools For Test Automation

Primary Tools For Test Automation According to the Gartner report...

EDITOR PICKS


Spread the love