Simplify WordReader

This commit is contained in:
Charles Gould 2017-10-15 23:39:53 -04:00
parent a548002598
commit 7f4ee4da65
2 changed files with 9 additions and 27 deletions

View File

@ -1,38 +1,21 @@
package lingo.common;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class WordReader {
private static void readFileToCollection(String filename, Collection<String> c) throws IOException {
try (final InputStream stream = WordReader.class.getResourceAsStream(filename);
final InputStreamReader streamReader = new InputStreamReader(stream);
final BufferedReader bufferedReader = new BufferedReader(streamReader)) {
String line = null;
while ((line = bufferedReader.readLine()) != null) {
c.add(line);
}
}
public static List<String> readFileToList(String filename) throws Exception {
final URI resource = WordReader.class.getResource(filename).toURI();
return Files.readAllLines(Paths.get(resource));
}
public static List<String> readFileToList(String filename) throws IOException {
final List<String> list = new ArrayList<>();
readFileToCollection(filename, list);
return list;
}
public static Set<String> readFileToSet(String filename) throws IOException {
final Set<String> list = new HashSet<>();
readFileToCollection(filename, list);
return list;
public static Set<String> readFileToSet(String filename) throws Exception {
return new HashSet<>(readFileToList(filename));
}
}

View File

@ -1,6 +1,5 @@
package lingo.server;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@ -17,7 +16,7 @@ public class WordRepository {
private final List<String> words;
public WordRepository() throws IOException {
public WordRepository() throws Exception {
guesses = WordReader.readFileToSet("/guesses.txt");
words = WordReader.readFileToList("/words.txt");
}