View RSS Feed

My Java Tips

Buffered Streams

Rate this Entry
by , 11-11-2011 at 06:33 PM (1789 Views)
When we use unbuffered I/O, each read or write request is handled directly by the underlying OS. This can make a program much less efficient, since each such request often triggers disk access, network activity, or some other operation that is relatively expensive. To reduce this kind of overhead, the Java platform implements buffered I/O streams.


Class BufferedReader reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines. The buffer size may be specified, or the default size may be used. The default is large enough for most purposes.

In general, each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream. It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly, such as FileReaders and InputStreamReaders. For example,

Java Code:
BufferedReader in = new BufferedReader(new FileReader("abc.in"));
will buffer the input from the specified file. Without buffering, each invocation of read() or readLine() could cause bytes to be read from the file, converted into characters, and then returned, which can be very inefficient.

Programs that use DataInputStreams for textual input can be localized by replacing each DataInputStream with an appropriate BufferedReader.

Below is the list of the BufferedReader class constructors.

BufferedReader(Reader in)
Creates a buffering character-input stream that uses a default-sized input buffer.

BufferedReader(Reader in, int sz)
Creates a buffering character-input stream that uses an input buffer of the specified size.

Below is the list of methods of the BufferedReader class.

void close()
Closes the stream and releases any system resources associated with it.

void mark(int readAheadLimit)
Marks the present position in the stream.

boolean markSupported()
Tells whether this stream supports the mark() operation, which it does.

int read()
Reads a single character.

int read(char[] cbuf, int off, int len)
Reads characters into a portion of an array.

String readLine()
Reads a line of text.

boolean ready()
Tells whether this stream is ready to be read.

void reset()
Resets the stream to the most recent mark.

long skip(long n)
Skips characters.

Example:

This program uses read() method to read characters and readLine() method to read lines from console. Please also note that method read() and readLine() both throws IOException.

Java Code:
import java.io.*;

class BufferedReaderCharactersLines {
	public static void main(String args[]) throws IOException {
		char c;
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Enter characters, 'q' to quit.");

		do {
			c = (char) br.read();
			System.out.print(c);
		} while (c != 'q');

		System.out.println("nEnter lines of text.");
		System.out.println("Enter 'stop' to quit.");
		String str;
		do {
			str = br.readLine();
			System.out.println(str);
		} while (!str.equals("stop"));
	}
}
Output:


Enter characters, 'q' to quit.
Jingle Bells
Jingle Bells
q
q

Enter lines of text.
Enter 'stop' to quit.
Jingle Bells
Jingle Bells
stop
stop

Submit "Buffered Streams" to Facebook Submit "Buffered Streams" to Digg Submit "Buffered Streams" to del.icio.us Submit "Buffered Streams" to StumbleUpon Submit "Buffered Streams" to Google

Tags: None Add / Edit Tags
Categories
Streams

Comments