coderolls Staff
coderolls Staff Hi 👋🏼, At coderolls we write tutorials about Java programming language and related technologies.

How To Read File Using BufferedReader In Java?

How To Read File Using BufferedReader In Java?

In this article, we will see how to read a file using the BufferedReader class in Java.

BufferedReader class reads text from a character-input stream. Because of buffering characters, it provides an efficient way to read characters, arrays, and lines.

BufferedReader provides two important methods to read from the file. i.e read() and readLine().

You can specify the bufferSize in BufferedReader constructer. But as motioned in the docs,

The default is large enough for most purposes.

BufferedReader read() method

BufferedReader read() method reads a single character. It returns the int representation of the char in the range of 0 to 65535 (0x00-0xffff), or -1 if the end of the stream has been reached.

We can cast the int value returned by the read() method to char to get the character value.

I have given an example of reading a file character by character using the read() method of the BufferedReader class

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
package com.coderolls;

import java.io.*;

/**
 * A Java program to read files character by character using the
 * read() method of the BufferedReader Class.
 * 
 * @author Gaurav Kukade at coderolls.com
 */
public class BufferedReaderReadMethodExample {

  public static void main(String[] args) {
    BufferedReader bufferedReader = null;
    try {
      bufferedReader = new BufferedReader(new FileReader("F:\\sample-text.txt"));
      int i;
      //read each character using read() method and print it
      while((i=bufferedReader.read())!=-1){  
        System.out.print((char)i);  
      }
    }catch (IOException e) {
      e.printStackTrace();
    }finally {
      try {
        bufferedReader.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}

Output

1
Welcome to coderolls.com!

See this example on GitHub.

BufferedReader readLine() method

As specified in the name, this method reads a line of text.

A line is considered to be terminated by any one of a line feed (‘\n’) or a carriage return (‘\r’).

The readLine() method returns the content of a line as a string except the line terminating character (i.e. \n or \r). Or it will return null if the end of the stream has been reached.

I have given below an example of reading file line by line using the readLine() method

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
package com.coderolls;

import java.io.*;

/**
 * A Java program to read files line by line using the
 * readLine() method of the BufferedReader Class.
 * 
 * @author Gaurav Kukade at coderolls.com
 *
 */
public class BufferedReaderReadLineMethodExample {

  public static void main(String[] args) {
    BufferedReader bufferedReader = null;
    try {
      bufferedReader = new BufferedReader(new FileReader("F:\\sample-text-two-lines.txt"));
      String line;
      //read each line using readLine() method and print it
      while((line = bufferedReader.readLine()) != null){  
        System.out.println(line);  
      }
    }catch (IOException e) {
      e.printStackTrace();
    }finally {
      try {
        bufferedReader.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}

Output

1
2
3
Welcome to coderolls.com!

Visit coderolls to read more coding tutorials!

See this example on GitHub.

I have given below a combined example of the Java BufferedReader read() and readLine() methods below

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
package com.coderolls;

import java.io.*;

public class BufferedReaderExanple {

  public static void main(String[] args) {
    BufferedReader bufferedReader = null;
    try {
      bufferedReader = new BufferedReader(new FileReader("F:\\sample-text.txt"));
      System.out.println("Read file using read() method: ");
      readFileCharacterByCharacter(bufferedReader);
      
      bufferedReader = new BufferedReader(new FileReader("F:\\sample-text-two-lines.txt"));
      System.out.println("\n\nRead file using readLine() method: ");
      readFileLineByLine(bufferedReader);
    
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
      e.printStackTrace();
    }

    try {
      bufferedReader.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
	
	/**
	 * A method to read file content character by character using the BufferedReader 
	 * read() method
	 * 
	 * @param bufferedReader
	 */
  public static void readFileCharacterByCharacter(BufferedReader bufferedReader) {
    try {
      int i;
      //read each character using read() method and print it
      while((i=bufferedReader.read())!=-1){  
        System.out.print((char)i);  
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
	
	/**
	 * A method to read file content line by line using the BufferedReader 
	 * readLine() method
	 * 
	 * @param bufferedReader
	 */
	public static void readFileLineByLine(BufferedReader bufferedReader) {
		
  try {
    String line;
    //read each line using readLine() method and print it
    while((line = bufferedReader.readLine()) != null){  
      System.out.println(line);  
    } 
    }catch (IOException e) {
      e.printStackTrace();
    }	
  }
}

See this example on GitHub.

newBufferedReader() method in Java 8

In Java 1.8 and above you can get a BufferedReader instance using the newBufferedReader() method of the java.nio.file.Files class.

Conclusion

You can read file character by character using the read() method of the BufferedReaderClass.

The read() method returns an integer value, you have to cast it to char to get the character value.

Also, you can read files line by line using the readLine() method of the BufferedReaderClass

The readLine() method returns the line content as a string, except the line terminating character

You can visit my YouTube channel ‘coderolls’ to find more video tutorials.

Please write down your thoughts in the comment section below.

Join Newsletter
Get the latest tutorials right in your inbox. We never spam!

comments powered by Disqus