Latent java.IOException:Write is closed errors when using BufferedWrite


Last Updated: 2010-12-01 20:32:18
  1. scott1234

    scott1234 New Member

    OK, I'm just starting out with my first non-trivial app using Eclipse and I'm having some issues. I'm trying to write accelerometer data to a file on the sd-card.

    In my Activity class I have a couple of things for file access:
    BufferedWriter out;
    FileWriter gpxwriter;

    Inside onCreate I set them up and do a test write to the file:
    Code (Text):
    1. Log.d("AccelerometerText", "onCreate ... Done");
    2. File gpxfile = new File(root, "RawAccelData2.txt");
    3. FileWriter gpxwriter = new FileWriter(gpxfile);
    4. out = new BufferedWriter(gpxwriter);
    5. out.write("Time (ms); X Value (g);Y Value(g);Z Value (g)");
    This code works, and I can open the file and see the text. great. If I exit at this point all is fine and dandy.

    But.... my activity class implements SensorEventListener, so I've implemented the following which gets called every time the sensor data changes. At that point I can see the accel data get updated in real time on the screen using settext calls. works great.....

    Code (Text):
    1. public void onSensorChanged(SensorEvent sensorEvent) {
    2.        synchronized (this) {
    3.                  long mytime = 0; //System.currentTimeMillis();
    4.                 textAccelerationX.setText("X Acceleration time:" + mytime + " " + Float.toString(sensorEvent.values[0]));
    5.                 textAccelerationY.setText("Y Acceleration time:" + mytime + " " + Float.toString(sensorEvent.values[1]));
    6.                 textAccelerationZ.setText("Z Acceleration time:" + mytime + " " + Float.toString(sensorEvent.values[2]));
    7.                  
    8.                 BigBuf[runningCount] = Float.toString(sensorEvent.values[0]);
    9.                                  
    10.                 if(runningCount==99) {
    11.                     runningCount=0;
    12.                     EnterCount++;
    13.                     Log.d("onSensorChanged", "EnterCount " + EnterCount);
    14.                     Log.d("onSensorChanged", "out " + out.toString());
    15.                     try {
    16.                        [B] out.write("Hello");
    17.                         out.newLine();[/B]
    18.                         } catch (IOException e) {
    19.                         // TODO Auto-generated catch block
    20.                         e.printStackTrace();
    21.                     }
    22.             }
    23.             runningCount++;
    24.        }
    25.        }
    I let the app go for a few minutes and hit the exit button which executes:
    Code (Text):
    1. case R.id.ButtonExit:
    2.             Log.d("AccelerometerText", "onClick ... buttonexit");
    3.             RunningZaccel+=0.8;
    4.             try {
    5.                 out.flush();
    6.                 out.close();
    7.             } catch (IOException e) {
    8.                 // TODO Auto-generated catch block
    9.                 e.printStackTrace();
    10.             }
    11.             this.finish();
    12.             break;
    13.         }


    I can see all the events get updated in my LogCat window using DDMS.

    The problem is that as soon as I hit my 'exit' button, I get a flurry on java.IO.BufferedWriter java.io.IOExceptions and these continue to scroll by even after the app has been exited and off the screen for minutes:

    Code (Text):
    1. D/onSensorChanged(15160): EnterCount 1541
    2. D/onSensorChanged(15160): out java.io.BufferedWriter@44763778
    3. W/System.err(15160): java.io.IOException: Writer is closed.
    4. W/System.err(15160):    at java.io.BufferedWriter.write(BufferedWriter.java:308)
    5. W/System.err(15160):    at java.io.Writer.write(Writer.java:152)
    6. W/System.err(15160):    at com.example.AccelerometerTest.AccelerometerTest.onSensorChanged(AccelerometerTest.java:202)
    7. W/System.err(15160):    at android.hardware.SensorManager$ListenerDelegate$1.handleMessage(SensorManager.java:449)
    8. W/System.err(15160):    at android.os.Handler.dispatchMessage(Handler.java:99)
    9. W/System.err(15160):    at android.os.Looper.loop(Looper.java:123)
    10. W/System.err(15160):    at android.app.ActivityThread.main(ActivityThread.java:4627)
    11. W/System.err(15160):    at java.lang.reflect.Method.invokeNative(Native Method)
    12. W/System.err(15160):    at java.lang.reflect.Method.invoke(Method.java:521)
    13. W/System.err(15160):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    14. W/System.err(15160):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    15. W/System.err(15160):    at dalvik.system.NativeStart.main(Native Method)
    16. D/dalvikvm(15160): GC_FOR_MALLOC freed 7981 objects / 523992 bytes in 65ms
    17. D/onSensorChanged(15160): EnterCount 1542
    18.  
    I obviously doing something wrong, any ideas?
    Am I not closing the file correctly?
    Why do I see IO errors even after the app is long gone and no longer running?
    can I save data in this manner (there are going to of file accesses)?

    Advertisement

Share This Page