-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleFile.java
More file actions
307 lines (254 loc) · 8.66 KB
/
SimpleFile.java
File metadata and controls
307 lines (254 loc) · 8.66 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import java.io.*;
import java.net.*;
import java.util.*;
/**
* This is a simple class for reading or writing text files in Java.
*
@author Andrew Merrill, Spring 2004, last updated January 2017
@version 2.0
<pre>
Example of writing to a file:
// create a new SimpleFile object
SimpleFile file = new SimpleFile("mytestfile.txt");
// tell it that you want to start writing
file.startWriting();
// or use file.startAppending() to write to the end of the file
// get a PrintStream from the file
PrintStream stream = file.getPrintStream();
// Use the println method from the PrintStream class to write output to the file
stream.println("line 1");
stream.println("line 2");
// tell the file that you are done writing
file.stopWriting();
///////////////////////////////////////////////////////////////////////////
Example of reading from a file:
// create a new SimpleFile object
SimpleFile file = new SimpleFile("mytestfile.txt");
for (String line : file) {
// do stuff with the line of text
}
// OR, if you want to do it the long way, you can do this:
// tell it that you want to start reading
file.startReading();
// the hasMoreLines() method returns true if there are more lines to read
while (file.hasMoreLines())
{
// the readNextLine() method reads the next line and returns it as a String
String line = file.readNextLine();
// now do whatever you want with the line you just read
// ...
}
// tell the file that you are done reading
file.stopReading();
</pre>
*/
public class SimpleFile extends File implements Iterable<String>
{
private PrintStream outputStream = null;
private BufferedReader inputReader = null;
private String line = null;
/*************************************************************************
* Construct a new SimpleFile from a filename.
*
* A SimpleFile may be used for either reading or writing.
*
* @param filename The filename of the file.
*/
public SimpleFile(String filename)
{
super(filename);
}
/*************************************************************************
* Construct a new SimpleFile from a parent directory and a filename.
*
* A SimpleFile may be used for either reading or writing.
*
* @param parent The directory in which to create this file.
* @param filename The filename of the file.
*/
public SimpleFile(String parent, String filename)
{
super(parent, filename);
}
/*************************************************************************
* Construct a new SimpleFile from a parent directory and a filename.
*
* A SimpleFile may be used for either reading or writing.
*
* @param parent The directory in which to create this file.
* @param filename The filename of the file.
*/
public SimpleFile(File parent, String filename)
{
super(parent, filename);
}
/*************************************************************************
Prepare the file for writing.
New lines will be written at the beginning of the file, erasing the old contents.
*/
public void startWriting()
{
startWriting(false);
}
/*************************************************************************
Prepare the file for appending.
This can be used instead of startWriting if you want
new lines to be written at the end of the file and to preserve the old contents.
*/
public void startAppending()
{
startWriting(true);
}
///////////////////////////////////////////////////////////////////////////
// private method that really prepares the file for writing
// append is true if we should append to the file, and false if we should erase it
private void startWriting(boolean append)
{
if (inputReader != null) {
throw new SimpleFileException("Cannot write to a file while reading it");
}
if (outputStream != null) {
throw new SimpleFileException("You need to stop writing first");
}
try {
FileOutputStream fileOutputStream = new FileOutputStream(this, append);
outputStream = new PrintStream(fileOutputStream);
}
catch (IOException exception)
{
outputStream = null;
throw new SimpleFileException("Failed to start writing: " + exception.getMessage());
}
}
/*************************************************************************
* Returns a PrintStream that can be used to write to the file.
* You must call startWriting or startAppending before calling this method.
*/
public PrintStream getPrintStream()
{
if (outputStream == null) {
throw new SimpleFileException("You need to start writing first");
}
return outputStream;
}
/*************************************************************************
* Closes the file when you are done writing to it.
*/
public void stopWriting()
{
if (outputStream != null) {
outputStream.close();
outputStream = null;
}
}
/*************************************************************************/
/**
* Prepare the file for reading.
*/
public void startReading()
{
if (inputReader != null) {
throw new SimpleFileException("You need to stop reading first");
}
if (outputStream != null) {
throw new SimpleFileException("You cannot read a file while you are writing it");
}
try {
FileReader fileReader = new FileReader(this);
inputReader = new BufferedReader(fileReader);
// a BufferedReader can read a file one line at a time
line = inputReader.readLine();
}
catch (IOException exception)
{
inputReader = null;
throw new SimpleFileException("Failed to start reading: " + exception.getMessage());
}
}
/*************************************************************************
* Returns true if there are more lines to read in the file.
* Returns false if we have reached the end of the file.
*/
public boolean hasMoreLines()
{
if (inputReader == null) {
throw new SimpleFileException("You need to start reading first");
}
if (line != null)
return true;
else
return false;
}
/*************************************************************************
* Returns the next line from the file, as a String.
*/
public String readNextLine()
{
if (inputReader == null) {
throw new SimpleFileException("You need to start reading first");
}
if (line == null) {
throw new SimpleFileException("There are no more lines to read");
}
String nextLine = line;
try {
line = inputReader.readLine();
}
catch (IOException exception)
{
throw new SimpleFileException("Failed while reading: " + exception.getMessage());
}
return nextLine;
}
/*************************************************************************
* Closes the file when you are done reading it.
*/
public void stopReading()
{
if (inputReader != null) {
try {
inputReader.close();
inputReader = null;
}
catch (IOException exception)
{
inputReader = null;
throw new SimpleFileException("Error when trying to close file: " + exception.getMessage());
}
}
}
/////////////////////////////////////////////////////////////////
public Iterator<String> iterator()
{
return new SimpleFileIterator();
}
class SimpleFileIterator implements Iterator<String>
{
SimpleFileIterator()
{
startReading();
}
public boolean hasNext()
{
boolean moreLines = hasMoreLines();
if (! moreLines) stopReading();
return moreLines;
}
public String next()
{
return readNextLine();
}
public void remove()
{
throw new UnsupportedOperationException("cannot remove from simple file iterator");
}
}
//////////////////////////////////////////////////////////////////////////////////////////
public class SimpleFileException extends RuntimeException
{
SimpleFileException(String message)
{
super(message + " with file " + SimpleFile.this.getName());
}
}
}