How to add data to an existing .txt file in android -
i developing application reads data coming accelerometer , want save them in .txt file, can later process them.
so far have been able readings , save 1 reading, understand create new file overwrites existing file. want have measurements moment push start button, until push stop button.
here 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, job gets done 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 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 have why can not store .txt file in sdcard , saved in internal memory.
thank time , help
try code. set second argument of filewriter true. can append existing file
file file = new file("hello.txt"); file.createnewfile(); filewriter writer = new filewriter(file,true); writer.write("writes content file"); writer.flush(); writer.close();
Comments
Post a Comment