-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRead.java
More file actions
34 lines (29 loc) · 847 Bytes
/
Read.java
File metadata and controls
34 lines (29 loc) · 847 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.io.*;
import java.util.*;
public class Read {
private Scanner input;
public void openTextFile(String fileName) {
try {
input = new Scanner(new File(fileName));
} catch (FileNotFoundException ex) {
System.err.println("Error opening or creating file.");
}
}
public void readFromFile() {
try {
while (input.hasNextLine()) {
System.out.println(input.nextLine());
}
} catch (NoSuchElementException ex) {
System.err.println("File improperly formed.");
input.close();
} catch (IllegalStateException ex) {
System.err.println("Error reading from file.");
}
}
public void closeFile() {
if (input != null) {
input.close();
}
}
}