File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed
src/com/codefortomorrow/advanced/chapter14/examples Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change
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
+
You can’t perform that action at this time.
0 commit comments