-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadText.java
More file actions
43 lines (35 loc) · 1.11 KB
/
Copy pathReadText.java
File metadata and controls
43 lines (35 loc) · 1.11 KB
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
35
36
37
38
39
40
41
42
43
package carbookingsystem1;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class ReadText {
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.");
System.err.println(ex);
}}
public void readFromFile() {
try {
while (input.hasNextLine()) {
System.out.println(input.nextLine());
}
} catch (NoSuchElementException ex) {
System.err.println("File improperly formed.");
System.err.println(ex);
System.exit(1);
input.close();
} catch (IllegalStateException ex) {
System.err.println("Error reading from file.");
System.err.println(ex);
}
}
public void closeFile() {
if (input != null) {
input.close();
}
}
}