-1
嘿,我有以下方法,通過查看一個大的.txt文件並檢查單詞是否存在,檢查單詞是否是合法的單詞。此時該方法只能正常工作,如果.txt文件中的單詞在相同的行上,且彼此之間只有一個空格。有什麼辦法可以讓它逐行讀出單詞列表;如果每行有一個字。例如,如果.txt文件是面向這樣的:Java緩衝讀取器,逐行閱讀
字1
單詞2
這裏是我的方法:
private boolean isWord(String word){
try{
//search .txt file of valid words. !!Will only read properly if there is a single space between each word.
BufferedReader in = new BufferedReader(new FileReader("/Users/user/Documents/workspace/AnagramAlgorithm/src/words.txt"));
String str;
while ((str = in.readLine()) != null){
if (str.indexOf(word) > -1){
return true;
}
else{
return false;
}
}
in.close();
}
catch (IOException e){
}
return false;
}