【发布时间】:2017-05-03 00:43:43
【问题描述】:
我的方法读取并打印文件,但我无法将每个单词添加到ArrayList dict。
阅读器一次读取一个字符,所以我写的内容将每个字符添加到dict: [c,a,t,d,o,g] 当我想要 [cat,dog] 时。文本文件在自己的行上有单词;如何区分它们?
到目前为止我的代码:
public static List Dictionary() {
ArrayList <String> dict = new ArrayList <String>();
File inFile = new File("C:/Users/Aidan/Desktop/fua.txt");
FileReader ins = null;
try {
ins = new FileReader(inFile);
int ch;
while ((ch = ins.read()) != -1) {
System.out.print((char) ch);
dict.add((char) ch + "");
}
} catch (Exception e) {
System.out.println(e);
} finally {
try {
ins.close();
} catch (Exception e) {
}
}
return dict;
}
【问题讨论】:
-
假设有很好的例子和方法来解决这个问题。请关注post
标签: java string arraylist filereader tokenize