-1

I've an app that is writing data to a file. The first time I run it, it goes grand and writes 1000s of values to the file. Then I close the app using this code

finish();
System.exit(0);

Which happens when I hit the stop button.

When I run it again after stopping, say in a few minutes, it only writes a few values to a new file, over the same time frame.

Here is the code I use to write to the file:

public void write(String message) {

    try {
        if (out == null) {
            FileWriter datawriter = new FileWriter(file, true);
            out = new BufferedWriter(datawriter);
            //out.write("X Value, Y Value, Z Value \n");
        }
        if (file.exists()) {
            out.append(message);
            out.flush();    
        }

Any insight into why this is happening would be much appreciated.

Thanks

Matt Cain
  • 5,638
  • 3
  • 36
  • 45
ELSheepO
  • 305
  • 3
  • 9
  • 26

2 Answers2

0

On finishing do out.close(). It probably is a system deteriorating.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • instead of `finish(); System.exit(0);` ? – ELSheepO Mar 18 '13 at 15:59
  • No, before. What finish does I do not know. – Joop Eggen Mar 18 '13 at 18:02
  • It doesn't work, out.close needs a class or variable. I should point out that I'm closing the app in a different class and that the file writing is happening in another class. – ELSheepO Mar 19 '13 at 12:34
  • Then do `out.close(); out = null;` instead of `out.flush();`. In fact better remove the condition `if (file.exists())` as the open code would have thrown an exception otherwise. – Joop Eggen Mar 19 '13 at 12:56
  • What exception would it throw? It hasn't thrown any exception with the first test runs. The flush is needed because it's constantly writing to the file, closing it would be unefficient as it would need to be reopened again. – ELSheepO Mar 19 '13 at 12:59
  • This is the question that I read about it http://stackoverflow.com/a/9272608/1383281 – ELSheepO Mar 19 '13 at 13:05
  • Inefficient, but *not* closing is a possible leak on system level. – Joop Eggen Mar 19 '13 at 13:24
  • Which would probably be causing the problem, now it makes sense. Thanks. – ELSheepO Mar 19 '13 at 15:47
0

Do not call

System.exit(0);

Read this post to know why.

Also, you shouldn't need to call flush(), but just close() when you're done, before your call to finish().

Try with this code:

public void write(String message) {
    try {
        if (out == null) {
            FileWriter datawriter = new FileWriter(file, true);
            out = new BufferedWriter(datawriter);
        }
        out.append(message);
        out.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Community
  • 1
  • 1
minipif
  • 4,756
  • 3
  • 30
  • 39
  • I need to call that to flush the buffer, least thats what I thought it is needed for. It was crashing when I had close and not flush before. Reading that post I should just leave it as `finish();` ? – ELSheepO Mar 18 '13 at 19:42