BufferedWriter停止写作

时间:2013-04-01 19:34:38

标签: java bufferedwriter

我正在尝试重写此词典: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;
    }
}

它对Arraylist的排序很好,但是当我写Strings时,它不会写出8个单词。我尝试改变BufferedWriter上的缓冲区,发现较小的缓冲区有帮助,所以我把缓冲区设为1.我发现这个:Buffered Writer Java Limit / Issues并且每次写入和结束时都尝试刷新(之后甚至变化的缓冲区) 。我仍然得到80360个单词而不是80368.为什么不写出完整的单词列表?我是否必须使用另一个BufferedWriter?如果是这样,我如何在不覆盖已经写入的内容的情况下使用它?

2 个答案:

答案 0 :(得分:2)

您正在使用输入数据的随机字符:

for (int a = read.read(); a != -1; a = read.read()){

不要混用read()readLine()来电。只需使用readLine()并测试null。

另外,要编写结果,不要使用List.toString impl和讨厌的正则表达式替换,只需遍历列表并写一个单词后跟换行符。

答案 1 :(得分:1)

我认为问题在于:

 for (int a = 0; a <= 70000; a += 10000){
        write.write(toWrite.subList(a, a + 10000).toString().replaceAll("[\\[,\\]]", "").replaceAll(" ", "\n"));
        write.flush();
    }

你应该写.write(“\ n”);在冲洗之前。