1

I have a small jMonkey program with balls bouncing around. I want to record the 3d vectors of each ball every second.

When I run my code:

@Override
public void simpleUpdate(float tpf) {
    if(getTimer().getTimeInSeconds() >= 1) {
        out.write("\n" + count + "  ");
        out.write(ball1g.getLocalTranslation() + "  ");
        out.write(ball2g.getLocalTranslation() + "  ");
        out.write(ball3g.getLocalTranslation().toString());
        count++;
        getTimer().reset();
    }
}

My text file is completely blank. But when I run:

@Override
public void simpleUpdate(float tpf) {
    out.write("this can be anything bigger than one character wide");
    if(getTimer().getTimeInSeconds() >= 1) {
        out.write("\n" + count + "  ");
        out.write(ball1g.getLocalTranslation() + "  ");
        out.write(ball2g.getLocalTranslation() + "  ");
        out.write(ball3g.getLocalTranslation().toString());
        count++;
        getTimer().reset();
    }
}

it works, just with a ridiculous amount of characters in between my actual data.

out.write("");

does nothing, it has to be at least

out.write(" ");

or bigger.

Am I doing anything wrong? If not, how can I work around this but accomplish the same task?

caleb.breckon
  • 1,336
  • 18
  • 42

1 Answers1

2

Does you program have a clean exit? If your output is buffered and you only write to it once in a while, it might take a long time for it to flush. If you terminate the program before it flushed, and it does not close the stream properly, the buffer contents will never be flushed and the resulting file will be empty.

OTOH if you're always writing (the code outside the if will likely be called a lot more than the code inside) the buffer will fill more quickly, so there are better odds at least some data will be flushed.

Try explicitly calling out.flush() after the code finishes writing (in the end of the if) and see if that solves your problem.

mgibsonbr
  • 21,755
  • 7
  • 70
  • 112