You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
description: "In this article we will see how to read a file using the `BufferReader` class in Java."
description: "In this article we will see how to read a file using the `BufferedReader` class in Java."
---
In this article we will see how to read a file using the `BufferReader `class in Java.
In this article we will see how to read a file using the `BufferedReader `class in Java.
`BufferReader` class reads text from a character-input stream. Because of buffering characters it provides an efficient way to read characters, arrays, and lines.
`BufferedReader` class reads text from a character-input stream. Because of buffering characters it provides an efficient way to read characters, arrays, and lines.
`BufferReader` provides two important methods to read from the file. i.e `read()` and `readLine()`.
`BufferedReader` provides two important methods to read from the file. i.e `read()` and `readLine()`.
You can specify the bufferSize in `BufferReader `constructer. But as [motioned in the docs](https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html),
You can specify the bufferSize in `BufferedReader `constructer. But as [motioned in the docs](https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html),
>The default is large enough for most purposes.
##BufferReader `read()` method
##BufferedReader `read()` method
`BufferReader` `read()` method reads a single character. IT returns the `int` representation of the char in range of 0 to 65535 (0x00-0xffff), or -1 if the end of the stream has been reached.
`BufferedReader` `read()` method reads a single character. IT returns the `int` representation of the char in range of 0 to 65535 (0x00-0xffff), or -1 if the end of the stream has been reached.
We can cast `int` value returned by `read()` method to `char` to get the character value.
I have given an example to read a file character by character using the `read()` method of the `BufferReader` class
I have given an example to read a file character by character using the `read()` method of the `BufferedReader` class
```java
package com.coderolls;
import java.io.*;
/**
* A java program to read file character by character using the
* read() method of theBufferReader Class.
* read() method of theBufferedReader Class.
*
* @author Gaurav Kukade at coderolls.com
*/
public classBufferReaderReadMethodExample {
public classBufferedReaderReadMethodExample {
public static void main(String[] args) {
Expand DownExpand Up
@@ -64,9 +64,9 @@ Output
```
Welcome to coderolls.com!
```
See [this example on GitHub](https://github.com/coderolls/blogpost-coding-examples/blob/main/java-files-io/BufferReaderReadMethodExample.java).
See [this example on GitHub](https://github.com/coderolls/blogpost-coding-examples/blob/main/java-files-io/BufferedReaderReadMethodExample.java).
##BufferReader `readLine()` method
##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').
Expand All
@@ -82,12 +82,12 @@ import java.io.*;
/**
* A java program to read file line by line using the
* readLine() method of theBufferReader Class.
* readLine() method of theBufferedReader Class.
*
* @author Gaurav Kukade at coderolls.com
*
*/
public classBufferReaderReadLineMethodExample {
public classBufferedReaderReadLineMethodExample {
public static void main(String[] args) {
Expand DownExpand Up
@@ -119,31 +119,33 @@ Welcome to coderolls.com!
Visit coderolls to read more coding tutorials!
```
See [this example on GitHub](https://github.com/coderolls/blogpost-coding-examples/blob/main/java-files-io/BufferReaderReadLineMethodExample.java).
See [this example on GitHub](https://github.com/coderolls/blogpost-coding-examples/blob/main/java-files-io/BufferedReaderReadLineMethodExample.java).
I have given below a combine example of the Java `BufferReader` `read()` and `readLine()` method below
I have given below a combine example of the Java `BufferedReader` `read()` and `readLine()` method below
```java
package com.coderolls;
import java.io.*;
public classBufferReaderExanple {
public classBufferedReaderExanple {
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();
}
readFileCharacterByCharacter(bufferedReader);
readFileLineByLine(bufferedReader);
try {
bufferedReader.close();
} catch (IOException e) {
Expand All
@@ -152,7 +154,7 @@ public class BufferReaderExanple {
}
/**
* A method to read file content character by character using theBufferReader
* A method to read file content character by character using theBufferedReader
* read() method
*
* @param bufferedReader
Expand All
@@ -170,7 +172,7 @@ public class BufferReaderExanple {
}
/**
* A method to read file content line by line using theBufferReader
* A method to read file content line by line using theBufferedReader
* readLine() method
*
* @param bufferedReader
Expand All
@@ -190,19 +192,19 @@ public class BufferReaderExanple {
}
```
See [this example on GitHub](https://github.com/coderolls/blogpost-coding-examples/blob/main/java-files-io/BufferReaderExanple.java).
See [this example on GitHub](https://github.com/coderolls/blogpost-coding-examples/blob/main/java-files-io/BufferedReaderExanple.java).
## `newBufferedReader()` method in Java 8
In Java 1.8 and above you can get a `BufferReader` instance using the `newBufferedReader()` method of the `java.nio.file.Files` class.
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 `BufferReader`Class.
You can read file character by character using the `read()` method of the `BufferedReader`Class.
`read()` method returns an integer value, you have to cast it to `char` to get character value.
Also, you can read file line by line using the `readLine()` method of the `BufferReader`Class
Also, you can read file line by line using the `readLine()` method of the `BufferedReader`Class
`readLine()` methods returns the line content as string, except the line terminating character
Sorry, this file is invalid so it cannot be displayed.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.