我有一个 BufferedWriter 的问题。我正在阅读一个 50,000 字的词表,使用词干算法并创建一个仅包含词干的新词表。而不是这个包含任何词干的新文件,但它实际上只包含:
-
这是我的代码:
public static void main(String[] args) {
BufferedReader reader=null;
BufferedWriter writer=null;
try {
writer = new BufferedWriter(new FileWriter(new File("src/newwordlist.txt")));
HashSet<String> db = new HashSet<String>();
reader = new BufferedReader(new InputStreamReader(new FileInputStream("src/wordlist"),"UTF-8"));
String word;
int i=0;
while ((word=reader.readLine())!=null) {
i++;
Stemmer s= new Stemmer();
s.addword(word);
s.stem();
String stem =s.toString();
if(!db.contains(stem)){
db.add(stem);
writer.write(stem);
//System.out.println(stem);
}
}
System.out.println("Reduced file from " + i + " words to " + db.size());
reader.close();
writer.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
我在控制台上得到的输出是:
将文件从 58110 字减少到 28201
所以我知道它的工作原理。我也尝试过更改writer.write(stem);
为writer.write("hi");
,我仍然得到相同的输出newwordlist.txt
。我知道它不是 Stemmer 类的错,我尝试输出词干字符串(我在其中注释了代码)并且产生了正确的输出到控制台,所以错误一定出在作者身上,但我不明白是什么。
编辑 1
我将代码简化为:
BufferedReader reader=null;
BufferedWriter writer=null;
try {
writer = new BufferedWriter(new FileWriter(new File("src/newwordlist.txt")));
HashSet<String> db = new HashSet<String>();
reader = new BufferedReader(new InputStreamReader(new FileInputStream("src/wordlist.txt"),"UTF-8"));
String word;
int i=0;
while ((word=reader.readLine())!=null) {
i++;
if(!db.contains(word)){
db.add(word);
writer.write("hi");
}
}
System.out.println("Reduced file from " + i + " words to " + db.size());
reader.close();
writer.close();
} catch (IOException e1) {
e1.printStackTrace();
}
现在我得到控制台输出:
Reduced file from 58110 words to 58109
但是输出文件还是空白