-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextFile.java
149 lines (142 loc) · 4.03 KB
/
TextFile.java
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
import java.io.*;
public class TextFile {
/**
* Text File constructor. Open a file for reading, or create
* a file for writing. If we create a file, and a file already
* exists with that name, the old file will be removed.
* @param filename The name of the file to read from or write to
* @param readOrWrite 'w' or 'W' for an output file (open for writing),
* and 'r' or 'R' for an input file (open for reading)
*/
public TextFile(String filename, char readOrWrite)
{
try
{
if (readOrWrite == 'w' || readOrWrite == 'W')
{
inputFile = false;
file = new RandomAccessFile(filename, "rw");
} else if (readOrWrite == 'r' || readOrWrite == 'R')
{
inputFile = true;
file = new RandomAccessFile(filename, "r");
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
Huffman.Usage();
System.exit(0);
}
position = 0;
}
/**
* Checks to see if we are at the end of a file. This method is only
* valid for input files, calling EndOfFile on an output fill will
* cause the program to exit. (This method should probably really throw an
* exception instead of halting the program on an error, but I'm
* trying to make your code a little simplier)
* @return True if we are at the end of an input file, and false otherwise
*/
public boolean EndOfFile()
{
Assert.notFalse(inputFile,"EndOfFile only relevant for input files");
try
{
return position == file.length();
}
catch (Exception e)
{
System.out.println(e.getMessage());
System.exit(0);
}
return true;
}
/**
* Read in the next character from the input file
* This method is only valud for input files, and
* will throw an halt program execution if called on an outpt file.
* (This method should probably really throw an
* exception instead of halting the program on an error, but I'm
* trying to make your code a little simplier)
* This method will also halt execution if you try to read past the
* end of a file.
* @return The next character from an input file
*/
public char readChar()
{
char returnchar = 0;
try
{
Assert.notFalse(inputFile,"Can only read from input files!");
Assert.notFalse(!EndOfFile(),"Read past end of file!");
position++;
returnchar = (char) file.read();
}
catch (Exception e)
{
System.out.println(e.getMessage());
System.exit(0);
}
return returnchar;
}
/**
* Write a character to an output file. This method is only valid for
* output files, and will halt execution if called on an input file.
* (This method should probably really throw an
* exception instead of halting the program on an error, but I'm
* trying to make your code a little simplier)
* @param c The character to write to the output file.
*/
public void writeChar(char c)
{
try
{
Assert.notFalse(!inputFile,"Can only write to output files!");
file.write((byte) c);
}
catch (Exception e)
{
System.out.println(e.getMessage());
System.exit(0);
}
}
/**
* Close the file (works for input and output files). Output files will
* not be properly written to disk if this method is not called.
*/
public void close()
{
try
{
file.close();
}
catch (Exception e)
{
System.out.println(e.getMessage());
System.exit(0);
}
}
/**
* Rewind the input file to the beginning, so we can reread
* the file. Only valid for input files.
*/
public void rewind()
{
try
{
Assert.notFalse(inputFile,"Can only rewind input files!");
file.seek(0);
position = 0;
}
catch (Exception e)
{
System.out.println(e.getMessage());
System.exit(0);
}
}
private boolean inputFile;
private RandomAccessFile file;
private long position;
private char buffer;
}