BufferedReader Class

How to use Java Bufferedreader Buffered readers are preferable for more demanding tasks, such as file and streamed readers. It optimize input and output by reducing the number of calls to the native API . Buffering the reads allows large volumes to be read from disk and copied to much faster RAM (Random Access Memory) to increase performance over the multiple network communications or disk reads done with each read command otherwise.
BufferedReader br = new BufferedReader(new FileReader("test.txt"));

Buffer Size

The buffer size may be specified, or the default size may be used. The default is large enough for most purposes. The default buffer size of 8192 chars can be overridden by the creator of the stream. The following Java program read input from console and display it using BufferedReader:
import java.util.*; import java.io.*; public class TestClass{ public static void main(String[] args) { BufferedReader br = null; try { br = new BufferedReader(new InputStreamReader(System.in)); String str; str = br.readLine(); System.out.println("You entered :: " + str); } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null) br.close(); } catch (IOException ex) { ex.printStackTrace(); } } } }
The following java program read text from an external file and display the text using BufferedReader:
import java.util.*; import java.io.*; public class TestClass{ public static void main(String[] args) { BufferedReader br = null; try { String line; br = new BufferedReader(new FileReader("D:\\test.txt")); while ((line = br.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null) br.close(); } catch (IOException ex) { ex.printStackTrace(); } } } }