In this section we will discuss about the BufferedReader class in Java.
In this section we will discuss about the BufferedReader class in Java.In this section we will discuss about the BufferedReader class in Java.
BufferedReader class in java.io package is used for read character input stream from a buffer. Size of buffer is default but, can be specified. In most of the purposes default size of buffer is used. BufferedReader should be wrapped around the Reader which read() method takes more time to read for example FileReader, InputStreamReader etc.
Constructor of BufferedReader
BufferedReader(Reader in) | This constructor creates a BufferedReader with the default buffer size which
contains the reference of Reader. Syntax : public BufferedReader(Reader in) |
BufferedReader(Reader in, int sz) | This constructor creates a BufferedReader with the specified buffer size
which contains the reference of Reader. Syntax : public BufferedReader(Reader in, int sz) |
Methods in BufferedReader
Example :
An example is being given here where I have used the java.io.BufferedReader class to read the stream from buffer. In this example I have used a BufferedReader with the default size which contains the reference of Reader as FileReader which provides the file to read from. The BufferedReader keeps the data into buffer.
Source Code
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class JavaBufferedReaderExample { public static void main(String args[]) { BufferedReader br = null; try { br = new BufferedReader(new FileReader("file.txt")); int c ; System.out.println(); System.out.println("***** OUTPUT *****"); System.out.println(); while((c= br.read()) != -1) { System.out.print((char) c); } System.out.println(); } catch(Exception ex) { System.out.println(ex); } finally { if(br != null) { try { br.close(); } catch(IOException ioe) { System.out.println(ioe); } } }// end finally }// end main }// end class
Output
When you will execute the above example you will get the output as follows :
Ads