我正在尝试重写这个字典:dictionary.txt按长度排序,而不是按字母顺序排序。我有以下代码(在main(String[] args)中):
BufferedReader read = new BufferedReader(new FileReader(new File(DIC_READ_PATH)));
BufferedWriter write= new BufferedWriter(new FileWriter(DIC_WRITE_PATH),1);
ArrayList<String> toWrite = new ArrayList<String>();
for (int a = read.read(); a != -1; a = read.read()){
char c = (char) a;
toWrite.add("" + c + read.readLine());
}
read.close();
Collections.sort(toWrite, new MyComparator());
for (int a = 0; a <= 70000; a += 10000){
write.write(toWrite.subList(a, a + 10000).toString().replaceAll("[\\[,\\]]", "").replaceAll(" ", "\n"));
write.flush();
}
write.write(toWrite.subList(80000, toWrite.size()).toString().replaceAll("[\\[,\\]]", "").replaceAll(" ", "\n"));
write.close();MyComparator:
public class MyComparator implements Comparator<String> {
@Override
public int compare(String arg0, String arg1) {
// TODO Auto-generated method stub
if (arg0.length() == arg1.length()){
return arg0.compareTo(arg1);
}
return arg0.length() < arg1.length() ? -1 : +1;
}
}它可以很好地对数组列表进行排序,但当我编写字符串时,它不会写入8个单词。我尝试改变BufferedWriter上的缓冲区,发现较小的缓冲区会有所帮助,所以我将缓冲区设置为1。我发现了这一点:Buffered Writer Java Limit / Issues,并在每次我写入并在末尾关闭时尝试刷新(甚至后来改变了缓冲区)。我还是得到了80360个单词,而不是80368个。为什么它不能写出完整的单词列表?我必须使用另一个BufferedWriter吗?如果是这样,我怎样才能使用它而不覆盖已经写好的东西呢?
发布于 2013-04-02 03:49:31
您正在使用输入数据的随机字符:
for (int a = read.read(); a != -1; a = read.read()){不要将read()和readLine()调用混为一谈。只需使用readLine()并测试是否为空。
此外,要编写结果,请不要使用List.toString impl和讨厌的正则表达式替换,只需循环遍历列表并在后面写一个单词和换行符。
发布于 2013-04-02 03:53:28
我认为问题出在这里:
for (int a = 0; a <= 70000; a += 10000){
write.write(toWrite.subList(a, a + 10000).toString().replaceAll("[\\[,\\]]", "").replaceAll(" ", "\n"));
write.flush();
}您应该在刷新前使用write.write("\n");。
https://stackoverflow.com/questions/15750408
复制相似问题