-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextFile.java
More file actions
162 lines (144 loc) · 4.11 KB
/
TextFile.java
File metadata and controls
162 lines (144 loc) · 4.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Class to open a file and either read information from it or write information
* to it. If a file is opened in "read" mode we can only read information from
* it. Otherwise, we can only write information to it.
*
*/
public class TextFile {
// Method readChar() returns this value when the end of the file has been
// reached
public static final char EOF = 0;
private FileInputStream in; // This variable points to the input file
// private BufferedWriter out; // This variable points to the output file
private DataOutputStream out;
private String accessMode; // Determines whether we wish to read from or to
// write to the file
/**
* Checks that the specified name corresponds to an existing file and if so
* it opens the file
*
* @param name
* name of the input file
*/
public TextFile(String name, String mode) {
if (mode.equals("read")) {
File file;
file = new File(name);
accessMode = "read";
if (!file.exists()) {
System.out.println("File " + name + " does not exist.");
System.exit(0); // end program
}
if (!(file.isFile() && file.canRead())) {
System.out.println("file " + file.getName() + " cannot be read from.");
System.exit(0);
}
try {
in = new FileInputStream(file); // Get ready to read from the
// file
} catch (IOException e) {
System.out.println("Error opening file " + name);
}
} else {
FileOutputStream fw;
accessMode = "write";
try {
fw = new FileOutputStream(name);
out = new DataOutputStream(fw);
} catch (IOException e) {
System.out.println("Error opening output file " + name);
}
}
}
/**
* Writes a character to the output file, except the character used to mark end of file
*
* @param c
* Character to write to the output file
*/
public void writeChar(char c) {
char EOF_Marker = (char) 1;
try {
if (c != EOF_Marker && c != EOF)
out.write(c);
} catch (IOException e) {
System.out.println("Error writing to output file");
}
}
/**
* Writes a character to the output file, even the character used to mark end of file
*
* @param c
* Character to write to the output file
*/
public void writeAllChar(char c) {
try {
out.write(c);
} catch (IOException e) {
System.out.println("Error writing to output file");
}
}
/**
* Reads the next character from the input file. If all the characters have
* been read this method returns EOF
*
* @return Next character from the input file or EOF if the whole file
* has already been read.
*/
public char readChar() {
char c = 0;
try {
if (in.available() > 0) { // We have not reached the end of the file
c = (char) in.read();
}
} catch (IOException e) {
System.out.println("Error reading from file ");
System.exit(0); // Terminate the program
}
return c;
}
/**
* Reads characters from the current position of the input file until the end of line
* is reached. The String read is returned.
* @return String of characters from current position in input file until the end of line.
* returns null if there are no more characters to read from the file.
*/
public String readLine() {
String line = "";
char c;
try {
while (in.available() > 0) { // We have not reached the end of the
// file
c = (char) in.read();
if (((int) c != 13) && ((int) c != 10))
line = line + c;
if ((int) c == 10)
break;
}
if (in.available() == 0 && line.equals("")) line = null;
} catch (IOException e) {
System.out.println("Error reading from file ");
System.exit(0); // Terminate the program
}
return line;
}
/**
* Close the file to ensure that no information is lost.
*/
public void close() {
try {
if (accessMode.equals("read"))
in.close();
else
out.close();
} catch (IOException e) {
System.out.println("Error closing file ");
System.exit(0); // Terminate the program
}
}
}