blob: 024088eff8b246841ecdc861aefddb55a2fd9015 (
plain)
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
44
45
46
47
48
49
50
51
|
import java.io.*;
import java.util.Scanner;
/*
* @author
* Kian Agheli
*
* References:
*
* Date:
* 2024-05-08
*
* Purpose of class:
* Read from a file.
*/
class Reader {
private File file; // A Reader has-a file.
private String contents; // A Reader has-a set of contents.
public Reader(File file) {
this.file = file;
contents = null;
Scanner scan = null;
try {
/* Scan over the contents of the input file. */
scan = new Scanner(file);
/* Save to object. */
contents = scan.nextLine() + "\n";
while (scan.hasNextLine()) {
contents += scan.nextLine();
contents += "\n";
}
/* On exception, exit. */
} catch (Exception e) {
System.out.println(e.getMessage());
System.exit(1);
} finally {
if (scan != null) {
scan.close();
}
}
}
/**
* @return the contents of the file.
*/
String getContents() {
return contents;
}
}
|