-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileReader.java
More file actions
42 lines (33 loc) · 924 Bytes
/
Copy pathFileReader.java
File metadata and controls
42 lines (33 loc) · 924 Bytes
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
package proj5;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
/**
* Class to handle reading a given input file
*/
public class FileReader {
private Scanner myReader;
/**
* Initializes the class with the given file.
* @param filename
*/
public FileReader(String filename) {
try {
myReader = new Scanner(new File(filename));
myReader.useDelimiter("[^a-zA-Z#]+");
} catch (FileNotFoundException e) {
System.out.println("File not found.");
}
}
/**
* Reads and returns all the words in the file as a linked list of Strings
*/
public LinkedList<String> getWords() {
LinkedList<String> list = new LinkedList<>();
while (myReader.hasNext()) {
String nextExpression = myReader.next();
list.add(nextExpression);
}
return list;
}
}