Android 创建新文件时,缓冲写入程序加载损坏的数据

Android 创建新文件时,缓冲写入程序加载损坏的数据,android,file,corrupt,writer,buffered,Android,File,Corrupt,Writer,Buffered,基本上,我试图将加速度计数据捕获到一个txt文件中。代码将accel数据写入一个字符串,然后在每次更改时进行更新,这是经常发生的。我包括了一个切换按钮,它可以开始记录数据并将其写入文件。这就是它变得奇怪的地方。代码检查文件是否已经存在,然后在文件末尾添加一个数字以确保它没有覆盖旧数据。但是,它会创建一个充满垃圾或损坏数据的文件。我验证了如果该文件已经存在,那么它会将良好的数据写入该文件,但如果必须创建该文件,则会创建一个损坏的文件。代码如下所示。我不知道文件的损坏情况 package com.a

基本上,我试图将加速度计数据捕获到一个txt文件中。代码将accel数据写入一个字符串,然后在每次更改时进行更新,这是经常发生的。我包括了一个切换按钮,它可以开始记录数据并将其写入文件。这就是它变得奇怪的地方。代码检查文件是否已经存在,然后在文件末尾添加一个数字以确保它没有覆盖旧数据。但是,它会创建一个充满垃圾或损坏数据的文件。我验证了如果该文件已经存在,那么它会将良好的数据写入该文件,但如果必须创建该文件,则会创建一个损坏的文件。代码如下所示。我不知道文件的损坏情况

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{

   }
    }
我已在舱单上声明

"<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>"
“”
包含损坏数据的日志文件示例

#EO'MyÎ和¸Œ-`IÐ ïïDèèèèèèè¼( 1Þ û'æÁ9Û™íb_CONSOLE·óZub™


让我知道你的想法。

我有一个droid X,我发现当你连接droid X作为usb存储设备时,手机上创建和存储的任何东西都是加密的。这就是为什么当我查看数据时,它似乎已损坏。当我在PC模式下连接手机时,它允许我查看文件是的。我希望这能为其他人省去一些痛苦和痛苦。我重写了17次代码来修复一个不存在的错误。

我有一个droid X,我发现当你将droid X连接为usb存储设备时,手机上创建和存储的任何东西都会被加密。这就是为什么当我查看数据时,会出现这种情况红色表示已损坏。当我在PC模式下连接手机时,它允许我适当地查看文件。我希望这能为其他人节省一些痛苦和痛苦。我重写了17次代码,以修复一个不存在的错误