0

I have an array of Strings (in java) with 14534 elements. When I write them to a file (csv), the file stops after 13475 elements (in the middle of a line, but with no special characters that I can see).

I checked with the debugger, the array is indeed filled.

Should I use another method? In c++ I would write binary, but I am unsure on how to do it in java. Could it be a problem with the string not being properbly recognised?

FileWriter writer = new FileWriter("out.csv");

for (int i = 0; i < lines; i++){
    for (int j = 0; j < cols; j++){
        // writer.append(""+i);
        // writer.append("-");
        writer.append(""+numbers[j][i]);
        writer.append(';'); 
    }
    writer.append('\n');
}

The strange thing is, it writes about 12 lines further if I add the code to check the lines (in comments).

Any suggestions on what to try?

2 Answers 2

3

You're never closing your writer - I suspect the data's just stuck in a buffer.

Adding a writer.flush() call will probably show it to you - but really you should be using either a try-with-resources statement (if you're using Java 7), or a try/finally statement to always close the writer.

As well as making sure you write all the data to the file, it means you'll also release the file handle...

Sign up to request clarification or add additional context in comments.

Comments

3

Flush and/or close the writer before checking the contents of the file. In all likelihood the incomplete output is due to buffering.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.