File tree Expand file tree Collapse file tree 1 file changed +63
-0
lines changed
java-simple-text-file-reader/src/main/java/com/hmkcode Expand file tree Collapse file tree 1 file changed +63
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .hmkcode ;
2+
3+ import java .io .BufferedReader ;
4+ import java .io .FileNotFoundException ;
5+ import java .io .FileReader ;
6+ import java .io .IOException ;
7+
8+ public class TextFileReader {
9+
10+ private BufferedReader buffer ;
11+ private String currentLine = "" ;
12+ public TextFileReader (){
13+
14+ }
15+
16+ public void open (String file ){
17+
18+ try {
19+ close ();
20+
21+ buffer = new BufferedReader (new FileReader (file ));
22+
23+ } catch (FileNotFoundException e1 ) {
24+ e1 .printStackTrace ();
25+
26+ }
27+
28+ }
29+
30+ public void close (){
31+
32+ try {
33+ if (buffer != null ){
34+ buffer .close ();
35+ buffer = null ;
36+ }
37+ } catch (IOException e ) {
38+ e .printStackTrace ();
39+ }
40+
41+ }
42+
43+ public String readLine () throws Exception {
44+ if (buffer != null ){
45+ currentLine = buffer .readLine ();
46+
47+ if (currentLine == null )
48+ close ();
49+
50+ return currentLine ;
51+ }
52+ else
53+ throw new Exception ("No file to read..." );
54+ }
55+
56+ public String getCurrent (){
57+ return this .currentLine ;
58+ }
59+
60+ public boolean isReadable (){
61+ return (buffer != null && this .currentLine != null );
62+ }
63+ }
You can’t perform that action at this time.
0 commit comments