Skip to content

Commit 740f968

Browse files
committed
TextFileReader.java
TextFileReader.java
1 parent 8375d0b commit 740f968

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
}

0 commit comments

Comments
 (0)