Download java io BufferedReader Class in Formats
QR Code for this Page
java io BufferedReader Class

java io BufferedReader Class

java.io.BufferedReader Class

BufferedReader Class in Java

The BufferedReader class reads text from a character input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines. To perform console input use a byte stream. Today, using a byte stream to read console input is still acceptable.

For commercial applications, the preferred method of reading console input is to use a character oriented stream. This makes your program easier to internationalize and maintain. In Java, console input is accomplished by reading from System.in. To obtain a character based stream that is attached to the console, wrap System.in in a BufferedReader object. BufferedReader supports a buffered input stream.

System.in refers to an object of type InputStream, it can be used for inputStream.

The following program shows how to get input from console using java.

How to read the characters from the console

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*  How to read the characters from the console Example
    Save with file name BufferedReaderExample.java  */
  
import java.io.*;
  
public class BufferedReaderExample
{
  public static void main(String args[])
  {
    //java.io.BufferedReader DECLARATION
    java.io.BufferedReader br = null;
    //InputStreamReader DECLARATION
    InputStreamReader ir = null;
    //WE SHOULD USE try-catch BECAUSE MOST OF THE
    //JAVA I/O CLASES THROWS IOException
    try
    {
      //DECLARE THE PREMITIVE DATA TYPE char
      char c;
      ir = new InputStreamReader(System.in);
      br = new BufferedReader(ir);
      System.out.println("Enter Characters, q to Quit : ");
      do
      {
        c = (char) br.read();
        System.out.println("Char : "+c);
      }while(c != 'q');
    }
    catch(Exception e)
    {
      System.out.println("Error Occurred : "+e.getMessage());
    }
    finally
    {
      //SHOULD CLOSE STREAMS FINALLY
      try
      {
        if(br!=null)
          br.close();
        if(ir!=null)
          ir.close();
      }
      catch(Exception ex)
      {
        System.out.println("Error Occurred : "+ex.getMessage());
      }
    }
  }
}

The above program output may look a little different from what you expected because System.in is line buffered, by default. This means that no input is actually passed to the program until you press ENTER. As you can guess, this does not make read() particularly valuable for interactive console input.

The following program demonstrates BufferedReader and the readLine() method; the program reads and displays lines of text until you enter the word "stop".

How to read the lines of text from the console

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*  How to read the lines of text from the console Example
    Save with file name BufferedReaderExample2.java */
  
import java.io.*;
  
public class BufferedReaderExample2
{
  public static void main(String args[])
  {
    //java.io.BufferedReader DECLARATION
    java.io.BufferedReader br = null;
    //InputStreamReader DECLARATION
    InputStreamReader ir = null;
    try
    {
      String str;
      ir = new InputStreamReader(System.in);
      br = new BufferedReader(ir);
      System.out.println("Enter some lines of text, stop to Quit : ");
      do
      {
        str = br.readLine();
        System.out.println("Line Text is : "+str);
      }while(!str.equals("stop"));
    }
    catch(Exception e)
    {
      System.out.println("Error Occurred : "+e.getMessage());
    }
    finally
    {
      //SHOULD CLOSE STREAMS FINALLY
      try
      {
        if(br!=null)
          br.close();
        if(ir!=null)
          ir.close();
      }
      catch(Exception ex)
      {
        System.out.println("Error Occurred : "+ex.getMessage());
      }
    }
  }
}

How to print only directory names using java? How to get only directories from given path using java?

How to read the text from file using Java BufferedReader

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*  How to read the text from file using Java BufferedReader Example
    Save with file name BufferedReaderExample3.java */
  
import java.io.*;
  
public class BufferedReaderExample3
{
  public static void main(String args[])
  {
    //java.io.BufferedReader DECLARATION
    java.io.BufferedReader br = null;
    //InputStreamReader DECLARATION
    InputStreamReader ir = null;
    //FileInputStream DECLARATION
    FileInputStream fis = null;
    try
    {
      int ch=0;
      fis = new FileInputStream("BufferedReaderExample3.java");
      ir = new InputStreamReader(fis);
      br = new BufferedReader(ir);
      while((ch = br.read()) != -1)
      {
        //CONVERT byte TO char
        System.out.print( (char) ch);
      }
    }
    catch(Exception e)
    {
      System.out.println("Error Occurred : "+e.getMessage());
    }
    finally
    {
      //SHOULD CLOSE STREAMS FINALLY
      try
      {
        if(br!=null)
          br.close();
        if(ir!=null)
          ir.close();
      }
      catch(Exception ex)
      {
        System.out.println("Error Occurred : "+ex.getMessage());
      }
    }
  }
}

How to print available file system root names using java? How to get file system roots using java? How to get available drives using java?

How to read total bytes from file into array using Java BufferedReader

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*  How to read total bytes from file into array
    using java BufferedReader Example
    Save with file name BufferedReaderExample4.java */
  
import java.io.*;
  
public class BufferedReaderExample4
{
  public static void main(String args[])
  {
    //java.io.BufferedReader DECLARATION
    java.io.BufferedReader br = null;
    //InputStreamReader DECLARATION
    InputStreamReader ir = null;
    //FileInputStream DECLARATION
    FileInputStream fis = null;
    //ARRAY TO READ BYTES
    char charbuff[] = null;
    try
    {
      fis = new FileInputStream("BufferedReaderExample4.java");
      ir = new InputStreamReader(fis);
      br = new BufferedReader(ir);
      //GET THE AVAILABLE BYTES FROM InputReader AND CREATES ARRAY
      charbuff = new char[fis.available()];
      //READ TOTAL BYTES FROM FILE
      br.read(charbuff, 0,fis.available());
      //PRINT CHARACTERS FROM ARRAY
      for(int j=0;j<charbuff.length;j++)
      {
        //CONVERT byte TO char
        System.out.print( (char) charbuff[j]);
      }
    }
    catch(Exception e)
    {
      System.out.println("Error Occurred : "+e.getMessage());
    }
    finally
    {
      //SHOULD CLOSE STREAMS FINALLY
      try
      {
        //RELEASE ARRAY MEMORY
        charbuff = null;
        if(br!=null)
          br.close();
        if(ir!=null)
          ir.close();
      }
      catch(Exception ex)
      {
        System.out.println("Error Occurred : "+ex.getMessage());
      }
    }
  }
}

How to read text line by line from file using Java BufferedReader

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*  How to read text line by line
    from file using java BufferedReader Example
    Save with file name BufferedReaderExample5.java */
  
import java.io.*;
  
public class BufferedReaderExample5
{
  public static void main(String args[])
  {
    //java.io.BufferedReader DECLARATION
    java.io.BufferedReader br = null;
    //InputStreamReader DECLARATION
    InputStreamReader ir = null;
    //FileInputStream DECLARATION
    FileInputStream fis = null;
    //String TO READ LINE FROM FILE
    String line = null;
    try
    {
      fis = new FileInputStream("BufferedReaderExample5.java");
      ir = new InputStreamReader(fis);
      br = new BufferedReader(ir);
      //GET THE LINE FROM FILE
      while((line = br.readLine()) != null)
      {
        //PRINT THE LINE
        System.out.println(line);
      }
    }
    catch(Exception e)
    {
      System.out.println("Error Occurred : "+e.getMessage());
    }
    finally
    {
      //SHOULD CLOSE STREAMS FINALLY
      try
      {
        //RELEASE MEMORY
        line = null;
        if(br!=null)
          br.close();
        if(ir!=null)
          ir.close();
      }
      catch(Exception ex)
      {
        System.out.println("Error Occurred : "+ex.getMessage());
      }
    }
  }
}

How to read bytes from file using Java BufferedReader

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*  How to read bytes from file using
    java BufferedReader Example
    Save with file name BufferedReaderExample6.java */
  
import java.io.*;
  
public class BufferedReaderExample6
{
  public static void main(String args[])
  {
    //java.io.BufferedReader DECLARATION
    java.io.BufferedReader br = null;
    //InputStreamReader DECLARATION
    InputStreamReader ir = null;
    //FileInputStream DECLARATION
    FileInputStream fis = null;
    //int TO READ byte FROM FILE
    int ch;
    try
    {
      fis = new FileInputStream("BufferedReaderExample6.java");
      ir = new InputStreamReader(fis);
      br = new BufferedReader(ir);
      int i = 1;
      //GET THE bytes FROM FILE
      while((ch = br.read()) !=-1)
      {
        //PRINT THE CHARACTER
        System.out.print((char) ch);
        if( (i%10) == 0)
          br.skip(3);
        i++;
      }
    }
    catch(Exception e)
    {
      System.out.println("Error Occurred : "+e.getMessage());
    }
    finally
    {
      //SHOULD CLOSE STREAMS FINALLY
      try
      {
        if(br!=null)
          br.close();
        if(ir!=null)
          ir.close();
      }
      catch(Exception ex)
      {
        System.out.println("Error Occurred : "+ex.getMessage());
      }
    }
  }
}

How to read bytes from file using java BufferedReader

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*  How to read bytes from file using
    java BufferedReader Example
    Save with file name BufferedReaderExample7.java */
  
import java.io.*;
  
public class BufferedReaderExample7
{
  public static void main(String args[])
  {
    //java.io.BufferedReader DECLARATION
    java.io.BufferedReader br = null;
    //InputStreamReader DECLARATION
    InputStreamReader ir = null;
    //FileInputStream DECLARATION
    FileInputStream fis = null;
    //int TO READ byte FROM FILE
    int ch;
    try
    {
      fis = new FileInputStream("BufferedReaderExample7.java");
      ir = new InputStreamReader(fis);
      br = new BufferedReader(ir);
      int i = 1;
      if(br.ready())
      {
          //GET THE bytes FROM FILE
          while((ch = br.read()) !=-1)
          {
            //PRINT THE CHARACTER
            System.out.print((char) ch);
            if(br.markSupported())
            {
              if(i<30)
              {
            if( (i%10) == 0)
              br.mark(i);
  
            if((i%20)==0)
            {
              br.reset();
            }
              }
              if((i%30)==0)
                break;
            }
            i++;
          }
      }
    }
    catch(Exception e)
    {
      System.out.println("Error Occurred : "+e.getMessage());
    }
    finally
    {
      //SHOULD CLOSE STREAMS FINALLY
      try
      {
        if(br!=null)
          br.close();
        if(ir!=null)
          ir.close();
      }
      catch(Exception ex)
      {
        System.out.println("Error Occurred : "+ex.getMessage());
      }
    }
  }
}
Download java io BufferedReader Class in Formats