BufferedReader

BufferedReader improves performance by buffering input. It has two constructors:

BufferedReader(Reader inputStream)
creates a buffered character stream using a default buffer size.
BufferedReader(Reader inputStream, int bufSize)
the size of the buffer is bufSize.
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.

Revised from Open JDK source code


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;

public class Main {

  public static void main(String[] args) throws Exception {
    URL url = new URL("http://localhost:1776");
    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
    String line;
    while ((line = in.readLine()) != null) {
      System.out.println(line);
    }
    in.close();
  }
}

Create BufferedReader from System.in


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class Main {
  public static void main(String[] args) throws Exception {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String strLine = in.readLine();
    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
    out.write(strLine, 0, strLine.length());
    out.flush();
    in.close();
    out.close();
  }
}

Create BufferedReader from InputStreamReader


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Standard {

  public static void main(String args[]) throws IOException {
    BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
    String number;
    int total = 0;

    while ((number = cin.readLine()) != null) {
      try {
        total += Integer.parseInt(number);
      } catch (NumberFormatException e) {
        System.err.println("Invalid number in input");
        System.exit(1);
      }
    }
    System.out.println(total);
  }
}

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

public class Main {
  public static void main(String[] argv) {
    try {
      BufferedReader br = new BufferedReader(new FileReader(new File("c:\\a.txt")));
      StringBuilder sb = new StringBuilder();
      String line = br.readLine();
      while (line != null) {
        sb.append(line + "\n");
        line = br.readLine();
      }
      br.close();
      System.out.println(sb.toString());
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
Home 
  Java Book 
    File Stream  

BufferedReader:
  1. BufferedReader
  2. new BufferedReader(FileReader fileReader)
  3. new BufferedReader(InputStreamReader input)
  4. BufferedReader: readLine()