Search code examples
androidfile-ioaccelerometer

How to add data to an existing .txt file in android


I am developing an application that reads data coming from the accelerometer and I want to save them in .txt file, so that I can later process them.

So far I have been able to get the readings and also to save just one reading, as from what I understand I always create a new file that overwrites the previously existing file. But I want is to have all the measurements from the moment I push the start button, until I push the stop button.

Here is the code I'm using:

public class MainActivity extends AppCompatActivity implements SensorEventListener, View.OnClickListener {

private SensorManager mSensorManager;

private Sensor mAccelerometer;
private Button bStart, bStop;
float[] acceleration = new float[3];
private String mString;

MyFile file = new MyFile(this);


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    bStart = (Button)findViewById(R.id.start);
    bStop = (Button)findViewById(R.id.stop);

    mSensorManager=(SensorManager)getSystemService(SENSOR_SERVICE);
    mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

    bStart.setOnClickListener(this);
    bStop.setOnClickListener(this);
    bStart.setEnabled(true);
    bStop.setEnabled(false);
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {}

public final void onSensorChanged(SensorEvent event){
    if(event.sensor.getType() ==  Sensor.TYPE_ACCELEROMETER){
        acceleration[0] = event.values[0];
        acceleration[1] = event.values[1];
        acceleration[2] = event.values[2];
        TextView mTextview1 = (TextView) findViewById(R.id.textView1);
        TextView mTextview2 = (TextView) findViewById(R.id.textView2);
        TextView mTextview3 = (TextView) findViewById(R.id.textView3);
        mTextview1.setText("X:"+String.valueOf( acceleration[0]));
        mTextview2.setText("Y:"+String.valueOf( acceleration[1]));
        mTextview3.setText("Z:"+String.valueOf( acceleration[2]));

        file.writeToSD(acceleration[0] + "," +acceleration[1] + "," +acceleration[2] + "\n");
    }
}

@Override
public void onClick(View v) {
    switch(v.getId()){
        case R.id.start:
            bStart.setEnabled(false);
            bStop.setEnabled(true);
            mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
            break;
        case R.id.stop:
            bStart.setEnabled(true);
            bStop.setEnabled(false);
            mSensorManager.unregisterListener(this);
            break;
    }
}

}

And MyFile Class, where the job gets done is the following:

public class MyFile {

String TAG = "MyFile";
Context context;

public MyFile(Context context) {
    this.context = context;
}

public Boolean writeToSD(String text) {
    Boolean write_successful = false;
    File root = null;
    try {
        // check for SDcard
        root = Environment.getExternalStorageDirectory();
        Log.i(TAG, "path.." + root.getAbsolutePath());

        //check sdcard permission
        if (root.canWrite()) {
            File fileDir = new File(root.getAbsolutePath());
            fileDir.mkdirs();

            File file = new File(fileDir, "samplefile.txt");
            FileWriter filewriter = new FileWriter(file);
            BufferedWriter out = new BufferedWriter(filewriter);
            out.append(text);
            out.flush();
            out.close();
            write_successful = true;
        }
    } catch (IOException e) {
       e.printStackTrace();
        write_successful = false;
    }
    return write_successful;
}

}

Another question that I have is why I can not store the .txt file in the sdcard and it is saved in the internal memory.

Thank you for your time and your help


Solution

  • Try this code. Set second argument of FileWriter to true. Thus you can append your existing file

     File file = new File("Hello.txt");    
     file.createNewFile();    
     FileWriter writer = new FileWriter(file,true); 
     writer.write("Writes the content to the file"); 
     writer.flush();
     writer.close();