Home  >  Core Java

java.io.BufferedReader Example in Java

By Arvind Rai, September 27, 2013
java.io.BufferedReader reads data from Reader. Reader can be StringReader , FileReader etc. BufferedReader reads data line wise as well as character wise. BufferedReader makes reading data easy. We can read data from file system using FileReader that takes input as File. StringReader provides the complete string to FileReader to read data. Find the example with StringReader.

BufferedReaderDemo.java
package com.concretepage.io;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
public class BufferedReaderDemo {
    public static void main(String[] args) {
        String str = "Hello World! \nThis is BufferedReader Program.";
        Reader sr = new StringReader(str);
        BufferedReader br = null;
        try {
            br = new BufferedReader(sr); 
            String s= br.readLine();
           while(s != null){ 
             System.out.println(s);  
             s=br.readLine();
           }
        } catch (IOException e) {
           e.printStackTrace();
        }finally {
            try {
                br.close();
              } catch (IOException ex) {
                ex.printStackTrace();
              }
        }
    }
}
 
Output:
Hello World! 
This is BufferedReader Program.
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us