-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileWordRead.java
75 lines (59 loc) · 1.75 KB
/
FileWordRead.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
package spellcheck;
import java.io.BufferedInputStream;
import java.io.IOException;
/** This class is for reading words form input file.
- First open input file for reading: in = new BufferedInputStream(new FileInputStream(String fileName));
- Next create an object of class FileWord Read: FileWordRead readWords = new FileWordRead(in);
- To read a word, first check that there is a next word in file: if ( readWords.hasNextWord() )
- Finally, to read a word, use: String nextWord = readWords.nextWord();
*/
public class FileWordRead{
private BufferedInputStream in;
private String nextWord;
private boolean endOfFile;
public FileWordRead(BufferedInputStream inFile) throws IOException
{
in = inFile;
endOfFile = false;
nextWord = readWord();
}
private String readWord()throws IOException{
int ch;
char nextChar;
StringBuffer buf = new StringBuffer();
ch = in.read();
if ( ch == -1 ){
endOfFile = true;
return(null);
}
nextChar = Character.toLowerCase((char) ch);
while ( ! (nextChar >= 'a' && nextChar <= 'z' )){
ch = in.read();
if ( ch == -1 ){
endOfFile = true;
return(null);
}
nextChar = Character.toLowerCase((char) ch);
}
while ( nextChar >= 'a' && nextChar <= 'z' ){
buf.append(nextChar);
ch = in.read();
if ( ch == -1 ){
endOfFile = true;
return(buf.toString());
}
nextChar = Character.toLowerCase((char) ch);
}
return (buf.toString());
}
public boolean hasNextWord() {
if ( nextWord != null ) return(true);
else return(false);
}
public String nextWord() throws java.io.IOException{
String toReturn = nextWord;
if ( !endOfFile ) nextWord = readWord();
else nextWord = null;
return(toReturn);
}
}