Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita9c9d60

Browse files
committed
Correct typo
1 parent5ad3d79 commita9c9d60

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,41 +1,41 @@
11
---
22
layout:post
3-
title:"How To Read File UsingBufferReader In Java?"
3+
title:"How To Read File UsingBufferedReader 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
---
1010

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

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

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

17-
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),
17+
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),
1818
>The default is large enough for most purposes.
1919
20-
##BufferReader`read()` method
20+
##BufferedReader`read()` method
2121

22-
`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.
22+
`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.
2323

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

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

3030
importjava.io.*;
3131

3232
/**
3333
* A java program to read file character by character using the
34-
* read() method of theBufferReader Class.
34+
* read() method of theBufferedReader Class.
3535
*
3636
*@author Gaurav Kukade at coderolls.com
3737
*/
38-
publicclassBufferReaderReadMethodExample {
38+
publicclassBufferedReaderReadMethodExample {
3939

4040
publicstaticvoidmain(String[]args) {
4141

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

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

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

8484
/**
8585
* A java program to read file line by line using the
86-
* readLine() method of theBufferReader Class.
86+
* readLine() method of theBufferedReader Class.
8787
*
8888
*@author Gaurav Kukade at coderolls.com
8989
*
9090
*/
91-
publicclassBufferReaderReadLineMethodExample {
91+
publicclassBufferedReaderReadLineMethodExample {
9292

9393
publicstaticvoidmain(String[]args) {
9494

@@ -120,31 +120,33 @@ Welcome to coderolls.com!
120120

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

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

127127
```java
128128
packagecom.coderolls;
129129

130130
importjava.io.*;
131131

132-
publicclassBufferReaderExanple {
132+
publicclassBufferedReaderExanple {
133133

134134
publicstaticvoidmain(String[]args) {
135135
BufferedReader bufferedReader=null;
136136
try {
137+
bufferedReader=newBufferedReader(newFileReader("F:\\sample-text.txt"));
138+
System.out.println("Read file using read() method:");
139+
readFileCharacterByCharacter(bufferedReader);
140+
137141
bufferedReader=newBufferedReader(newFileReader("F:\\sample-text-two-lines.txt"));
142+
System.out.println("\n\nRead file using readLine() method:");
143+
readFileLineByLine(bufferedReader);
138144

139145
}catch (FileNotFoundException e) {
140146
// TODO Auto-generated catch block
141147
e.printStackTrace();
142148
}
143-
144-
readFileCharacterByCharacter(bufferedReader);
145-
146-
readFileLineByLine(bufferedReader);
147-
149+
148150
try {
149151
bufferedReader.close();
150152
}catch (IOException e) {
@@ -153,7 +155,7 @@ public class BufferReaderExanple {
153155
}
154156

155157
/**
156-
* A method to read file content character by character using theBufferReader
158+
* A method to read file content character by character using theBufferedReader
157159
* read() method
158160
*
159161
*@param bufferedReader
@@ -171,7 +173,7 @@ public class BufferReaderExanple {
171173
}
172174

173175
/**
174-
* A method to read file content line by line using theBufferReader
176+
* A method to read file content line by line using theBufferedReader
175177
* readLine() method
176178
*
177179
*@param bufferedReader
@@ -191,19 +193,19 @@ public class BufferReaderExanple {
191193
}
192194
```
193195

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

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

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

200202
##Conclusion
201203

202-
You can read file character by character using the`read()` method of the`BufferReader`Class.
204+
You can read file character by character using the`read()` method of the`BufferedReader`Class.
203205

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

206-
Also, you can read file line by line using the`readLine()` method of the`BufferReader`Class
208+
Also, you can read file line by line using the`readLine()` method of the`BufferedReader`Class
207209

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

Loading

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp