2013-08-01 49 views
0

所以我正在開發android應用程序來存儲傳感器值在一個文件中。我的主要問題是,應用程序只是在文件中寫入最後一個值。基本上,我試圖編寫代碼來存儲每個傳感器值。在編寫Android應用程序的初學者...有人可以幫忙嗎?下面是代碼:Android寫入文件只是最後一個值

package com.example.hello; 

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.OutputStreamWriter; 

import android.app.Activity; 
import android.content.Context; 
import android.hardware.Sensor; 
import android.hardware.SensorEvent; 
import android.hardware.SensorEventListener; 
import android.hardware.SensorManager; 
import android.os.Bundle; 
import android.os.Environment; 
import android.view.Menu; 
import android.widget.TextView; 

public class MainActivity extends Activity { 

private TextView xAxis, yAxis, zAxis; 
private SensorManager sm; 
private Sensor mSensor; 
final File file = new File(Environment.getExternalStorageDirectory(), 
     "results.txt"); 
static FileOutputStream fos; 
static OutputStreamWriter myOutWriter; 
private String writeString; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    xAxis = (TextView) findViewById(R.id.xAxis); 
    yAxis = (TextView) findViewById(R.id.yAxis); 
    zAxis = (TextView) findViewById(R.id.zAxis); 

    sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE); 
    mSensor = sm.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); 
    try { 
     fos = new FileOutputStream(file); 
     myOutWriter = new OutputStreamWriter(fos); 
    } catch (FileNotFoundException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 

    SensorEventListener sensor = new SensorEventListener() { 

     @Override 
     public void onSensorChanged(SensorEvent event) { 
      xAxis.setText("xAxis: " + event.values[0]); 
      yAxis.setText("yAxis: " + event.values[1]); 
      zAxis.setText("zAxis: " + event.values[2]); 
      String test = new String(" " + event.values[0] + " " 
        + event.values[1] + " " + event.values[2]); 
      writeData(test); 
     } 

     @Override 
     public void onAccuracyChanged(Sensor sensor, int accuracy) { 
      // TODO Auto-generated method stub 
     } 
    }; 

    sm.registerListener(sensor, 
      sm.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), 
      SensorManager.SENSOR_DELAY_UI); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

public static void writeData(String test) { 
    try { 

     myOutWriter.append(test); 
     myOutWriter.flush(); 
     myOutWriter.close(); 
    } catch (FileNotFoundException e) { 
     // handle exception 
    } catch (IOException e) { 
     // handle exception 
    } 
} 

}

回答

0

你每次你寫,而無需再次打開它時關閉作家。將日誌記錄添加到異常中,而不是將它們留空,並且應該看到一些堆棧跟蹤。

東西沿着線: Log.e(TAG,消息,異常),所以: Log.e( 「MainActivity」, 「無法寫入的值到文件」,E);

0

也請考慮下面的構造方法FileOutputStream中

fos = new FileOutputStream(file,true); 
相關問題