Buffered Writer loads corrupt data when new file created
Pregunta
Basically, I'm trying to capture the accelerometer data into a txt file. The code writes the accel data into a string, and then updates everytime it changes which is often. I included a toggle button which starts the logging of the data and is supposed to write it into a file. this is where it gets weird. The code checks to see if the file exists already, and then adds a number to the end of the file to make sure it's not overwriting older data. BUT, it creates a file full of garbage or corrupted data. I verified that if the file is already existing, then it will write good data into the file, but if it has to create the file, it creates a corrupted file. Code is shown below. Don't know what is going on with the corruption of the file.
package com.accelerometermonitor;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class AccelerometerMonitorActivity extends Activity implements OnClickListener{
/** Called when the activity is first created. */
SensorManager sensorManager;
Sensor accel;
TextView xxaxistext;
TextView yyaxistext;
TextView zzaxistext;
Button toggle;
boolean recordstatus = false;
StringBuilder databuilder;
String data;
TextView testing;
String xstring;
String ystring;
String zstring;
int filecounter=0;
boolean notdone = true;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
accel = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
toggle=(Button)findViewById(R.id.togglebutt);
toggle.setOnClickListener(this);
xxaxistext=(TextView)findViewById(R.id.xaxistext);
yyaxistext=(TextView)findViewById(R.id.yaxistext);
zzaxistext=(TextView)findViewById(R.id.zaxistext);
testing=(TextView)findViewById(R.id.testtext);
}
@Override
public void onStart(){
super.onStart();
while (notdone) { //establishes existing logfile number to be used and stored
File logFile = new File("/sdcard/log"+""+ filecounter +".txt");
if (logFile.exists()){
filecounter++;
}
else {
notdone = false;
}
}
}
protected void onResume() {
super.onResume();
sensorManager.registerListener(accelListener, accel,
SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(accelListener);
}
private SensorEventListener accelListener = new SensorEventListener(){
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void onSensorChanged(SensorEvent event) {
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
update(x, y, z);
}
};
public void update (float x, float y, float z){//sets textview to appropriate number
xxaxistext.setText("X Axis:" +""+ x);
yyaxistext.setText("Y Axis:" +""+ y);
zzaxistext.setText("Z Axis:" +""+ z);
//save to string
data="";
xstring=("" + x);
ystring=("" + y);
zstring=("" + z);
data=data.concat(xstring + "," + ystring +"," + zstring +"\n");
// testing.setText("result:" + data);
appendLog(data, filecounter);
}
public void onClick(View target) {
if (recordstatus==false){
toggle.setText("Stop");
recordstatus = true;
appendLog(data, filecounter);
testing.setText("Logging");
}
else{
//Stop recording
recordstatus = false;
toggle.setText("Start");
testing.setText("logging stopped");
filecounter++;
}
}
public void appendLog(String text, int filecounter)
{
if(recordstatus==true){
File logFile = new File("/sdcard/log"+""+ filecounter +".txt");
testing.setText("working");
if (!logFile.exists())
{
try
{
logFile.createNewFile();
Toast toast= Toast.makeText(this,"new file created",0);
toast.show();
BufferedWriter buf1 = new BufferedWriter(new FileWriter(logFile , false));
buf1.write(13);
buf1.newLine();
buf1.flush();
buf1.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try
{
// File logaFile = new File("/sdcard/log"+""+ filecounter +".txt");
//BufferedWriter for performance, true to set append to file flag
//FileWriter logFile=new FileWriter(logaFile , false);
BufferedWriter buf = new BufferedWriter(new FileWriter(logFile , true));
buf.append(text);
buf.newLine();
buf.flush();
buf.close();
testing.setText("append working");
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else{
}
}
I have declared in my manifest
"<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>"
An example of a log file with corrupt data
#EO‘MyÎ&¸ Œ- `íÐ ïÇDêç³òß¼( 1Þ û'æÁ9Û™íb_CONSOLE ·óZub™
Let me know what you think.
Solución
I have a droid X and what I've found is that when you connect the droid x as a usb storage device, anything that is created and stored on the phone is ENCRYPTED. That is why when I was looking at the data, it appeared to be corrupt. when I hook the phone up in PC Mode, it then allows me to view the files appropriately. I hope that this saves someone else some pain and agony. I rewrote my code 17 times to fix an error that wasn't there.