Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f3535d7

Browse files
authored
Merge pull request #7 from coderolls/blog/read-file-using-bufferreader
Correct typo
2 parents a852581 + 34b3771 commit f3535d7

File tree

2 files changed

+32
-30
lines changed

2 files changed

+32
-30
lines changed

_posts/java-files-io/2021-01-03-read-file-using-bufferreader.md

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
11
---
22
layout: post
3-
title: "How To Read File Using BufferReader In Java?"
3+
title: "How To Read File Using BufferedReader In Java?"
44
author: gaurav
5-
image: assets/images/2021-01-03/read-file-using-bufferreader.png
5+
image: assets/images/2021-01-03/read-file-using-bufferedreader.png
66
categories: [Java, Java File IO]
7-
description: "In this article we will see how to read a file using the `BufferReader` class in Java."
7+
description: "In this article we will see how to read a file using the `BufferedReader` class in Java."
88
---
99

10-
In this article we will see how to read a file using the `BufferReader `class in Java.
10+
In this article we will see how to read a file using the `BufferedReader `class in Java.
1111

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

14-
`BufferReader` provides two important methods to read from the file. i.e `read()` and `readLine()`.
14+
`BufferedReader` provides two important methods to read from the file. i.e `read()` and `readLine()`.
1515

16-
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),
16+
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),
1717
>The default is large enough for most purposes.
1818
19-
## BufferReader `read()` method
19+
## BufferedReader `read()` method
2020

21-
`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.
21+
`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.
2222

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

25-
I have given an example to read a file character by character using the `read()` method of the `BufferReader` class
25+
I have given an example to read a file character by character using the `read()` method of the `BufferedReader` class
2626
```java
2727
package com.coderolls;
2828

2929
import java.io.*;
3030

3131
/**
3232
* A java program to read file character by character using the
33-
* read() method of the BufferReader Class.
33+
* read() method of the BufferedReader Class.
3434
*
3535
* @author Gaurav Kukade at coderolls.com
3636
*/
37-
public class BufferReaderReadMethodExample {
37+
public class BufferedReaderReadMethodExample {
3838

3939
public static void main(String[] args) {
4040

@@ -64,9 +64,9 @@ Output
6464
```
6565
Welcome to coderolls.com!
6666
```
67-
See [this example on GitHub](https://github.com/coderolls/blogpost-coding-examples/blob/main/java-files-io/BufferReaderReadMethodExample.java).
67+
See [this example on GitHub](https://github.com/coderolls/blogpost-coding-examples/blob/main/java-files-io/BufferedReaderReadMethodExample.java).
6868

69-
## BufferReader `readLine()` method
69+
## BufferedReader `readLine()` method
7070
As specified in the name, this method reads a line of text.
7171

7272
A line is considered to be terminated by any one of a line feed ('\n') or a carriage return ('\r').
@@ -82,12 +82,12 @@ import java.io.*;
8282

8383
/**
8484
* A java program to read file line by line using the
85-
* readLine() method of the BufferReader Class.
85+
* readLine() method of the BufferedReader Class.
8686
*
8787
* @author Gaurav Kukade at coderolls.com
8888
*
8989
*/
90-
public class BufferReaderReadLineMethodExample {
90+
public class BufferedReaderReadLineMethodExample {
9191

9292
public static void main(String[] args) {
9393

@@ -119,31 +119,33 @@ Welcome to coderolls.com!
119119

120120
Visit coderolls to read more coding tutorials!
121121
```
122-
See [this example on GitHub](https://github.com/coderolls/blogpost-coding-examples/blob/main/java-files-io/BufferReaderReadLineMethodExample.java).
122+
See [this example on GitHub](https://github.com/coderolls/blogpost-coding-examples/blob/main/java-files-io/BufferedReaderReadLineMethodExample.java).
123123

124-
I have given below a combine example of the Java `BufferReader` `read()` and `readLine()` method below
124+
I have given below a combine example of the Java `BufferedReader` `read()` and `readLine()` method below
125125

126126
```java
127127
package com.coderolls;
128128

129129
import java.io.*;
130130

131-
public class BufferReaderExanple {
131+
public class BufferedReaderExanple {
132132

133133
public static void main(String[] args) {
134134
BufferedReader bufferedReader = null;
135135
try {
136+
bufferedReader = new BufferedReader(new FileReader("F:\\sample-text.txt"));
137+
System.out.println("Read file using read() method: ");
138+
readFileCharacterByCharacter(bufferedReader);
139+
136140
bufferedReader = new BufferedReader(new FileReader("F:\\sample-text-two-lines.txt"));
141+
System.out.println("\n\nRead file using readLine() method: ");
142+
readFileLineByLine(bufferedReader);
137143

138144
} catch (FileNotFoundException e) {
139145
// TODO Auto-generated catch block
140146
e.printStackTrace();
141147
}
142-
143-
readFileCharacterByCharacter(bufferedReader);
144-
145-
readFileLineByLine(bufferedReader);
146-
148+
147149
try {
148150
bufferedReader.close();
149151
} catch (IOException e) {
@@ -152,7 +154,7 @@ public class BufferReaderExanple {
152154
}
153155

154156
/**
155-
* A method to read file content character by character using the BufferReader
157+
* A method to read file content character by character using the BufferedReader
156158
* read() method
157159
*
158160
* @param bufferedReader
@@ -170,7 +172,7 @@ public class BufferReaderExanple {
170172
}
171173

172174
/**
173-
* A method to read file content line by line using the BufferReader
175+
* A method to read file content line by line using the BufferedReader
174176
* readLine() method
175177
*
176178
* @param bufferedReader
@@ -190,19 +192,19 @@ public class BufferReaderExanple {
190192
}
191193
```
192194

193-
See [this example on GitHub](https://github.com/coderolls/blogpost-coding-examples/blob/main/java-files-io/BufferReaderExanple.java).
195+
See [this example on GitHub](https://github.com/coderolls/blogpost-coding-examples/blob/main/java-files-io/BufferedReaderExanple.java).
194196

195197
## `newBufferedReader()` method in Java 8
196198

197-
In Java 1.8 and above you can get a `BufferReader` instance using the `newBufferedReader()` method of the `java.nio.file.Files` class.
199+
In Java 1.8 and above you can get a `BufferedReader` instance using the `newBufferedReader()` method of the `java.nio.file.Files` class.
198200

199201
## Conclusion
200202

201-
You can read file character by character using the `read()` method of the `BufferReader`Class.
203+
You can read file character by character using the `read()` method of the `BufferedReader`Class.
202204

203205
`read()` method returns an integer value, you have to cast it to `char` to get character value.
204206

205-
Also, you can read file line by line using the `readLine()` method of the `BufferReader`Class
207+
Also, you can read file line by line using the `readLine()` method of the `BufferedReader`Class
206208

207209
`readLine()` methods returns the line content as string, except the line terminating character
208210

Loading

0 commit comments

Comments
 (0)