-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileReader.java
64 lines (62 loc) · 2.07 KB
/
FileReader.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
package proj4;
import java.io.*;
/**
* A FileReader object will read tokens from an input file. The name of
* the input file is given when the constructor is run. The lone method,
* nextToken(), will return the next token in the file as a String.
*
* @author Chris Fernandes
* @version 2/20/17
*/
public class FileReader
{
private StreamTokenizer st; //file descriptor
/** non-default constructor.
*
* @param fileName path to input file. Can either be a full
* path like "C:/project4/proj4_input.txt" or a relative one like
* "src/proj4_input.txt" where file searching begins inside
* the project folder.
* Be sure to use forward slashes to specify folders. And
* don't forget the quotes (it's a String!)
*/
public FileReader(String fileName)
{
try {
st = new StreamTokenizer(
new BufferedReader(
new InputStreamReader(
new FileInputStream(fileName))));
} catch (IOException e) {}
st.resetSyntax(); // remove default rules
st.ordinaryChars(0, Character.MAX_VALUE); // turn on all chars
st.whitespaceChars(0, 39); // ignore unprintables &
st.whitespaceChars(95, Character.MAX_VALUE); // everything after ^
}
/** Returns the next valid token from the input file.
* Possible tokens are "+", "-", "*", "/", "^", "(", ")",
* ";", operands (capital letters), and the special
* token "EOF" which is returned when the
* end of the input file is reached.
*
* @return next valid token or "EOF"
*/
public String nextToken()
{
try
{
while (st.nextToken() != StreamTokenizer.TT_EOF) {
if (st.ttype < 0)
{
return st.sval;
}
else
{
return String.valueOf((char)st.ttype);
}
}
return "EOF";
} catch (IOException e) {}
return "error on token read";
}
}