Skip to content

Commit cf4cc79

Browse files
committed
added source code for BufferedReader example in ch 14
1 parent 1c39543 commit cf4cc79

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.codefortomorrow.advanced.chapter14.examples;
2+
3+
import java.io.BufferedReader;
4+
import java.io.FileReader;
5+
import java.io.IOException;
6+
7+
/**
8+
* Source: https://beginnersbook.com/2014/01/how-to-read-file-in-java-using-bufferedreader/
9+
*/
10+
public class BufferedReaderExample {
11+
public static void main(String[] args) {
12+
13+
BufferedReader br = null;
14+
BufferedReader br2 = null;
15+
16+
try {
17+
br = new BufferedReader(new FileReader("B:\\myfile.txt"));
18+
19+
//One way of reading the file
20+
System.out.println("Reading the file using readLine() method:");
21+
String contentLine = br.readLine();
22+
while (contentLine != null) {
23+
System.out.println(contentLine);
24+
contentLine = br.readLine();
25+
}
26+
27+
br2 = new BufferedReader(new FileReader("B:\\myfile2.txt"));
28+
29+
//Second way of reading the file
30+
System.out.println("Reading the file using read() method:");
31+
int num=0;
32+
char ch;
33+
while((num=br2.read()) != -1)
34+
{
35+
ch=(char)num;
36+
System.out.print(ch);
37+
}
38+
39+
}
40+
catch (IOException ioe)
41+
{
42+
ioe.printStackTrace();
43+
}
44+
}
45+
}
46+

0 commit comments

Comments
 (0)