Justin
Justin

Reputation: 25287

BufferedWriter Stops Writing

I am trying to rewrite this dictionary: dictionary.txt sorted by length instead by alphabetical order. I have the following code (inside 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;
    }
}

It sorts the Arraylist fine, but when I write the Strings, it doesn't write 8 words. I tried varying the buffer on the BufferedWriter and found that smaller Buffers help, so I put a Buffer of 1. I found this: Buffered Writer Java Limit / Issues and tried flushing every time I write and closing at the end (even varied Buffer afterwards). I still get 80360 words instead of 80368. Why won't it write the full list of words? Do I have to use another BufferedWriter? If so, how can I use it without it overwriting what is already writen?

Upvotes: 0

Views: 655

Answers (2)

G&#225;bor Bakos
G&#225;bor Bakos

Reputation: 9100

I think the problem is here:

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

You should write.write("\n"); before flush.

Upvotes: 1

jtahlborn
jtahlborn

Reputation: 53694

you are consuming random characters of your input data:

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

don't mix read() and readLine() calls. just use readLine() and test for null.

also, to write your results, don't use the List.toString impl and nasty regex replacements, just loop over the list and write one word followed by a newline.

Upvotes: 2

Related Questions