Java I/O How to - Create Buffered Reader








Question

We would like to know how to create Buffered Reader.

Answer

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*from  w w  w . j  a va 2 s.  co  m*/
public class MainClass {
  public static void main(String[] args) throws IOException {
    BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Enter a line:");
    System.out.println(stdin.readLine());
  }
}

Create BufferedReader from FileReader

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
//from ww  w  . ja  v a  2s.co  m
public class MainClass {
  public static void main(String[] args) throws IOException {
    // 1. Reading input by lines:
    BufferedReader in = new BufferedReader(new FileReader("IOStreamDemo.java"));
    String s, s2 = new String();
    while ((s = in.readLine()) != null)
      s2 += s + "\n";
    in.close();

  }
}