자력 센서의 값을 표시하고 이러한 데이터를 파일에 저장하는 응용 프로그램의 코드를 작성하려고합니다. 다음 코드를 작성했지만 문제는 내 파일에서 마지막 값을 포함하는 한 줄만 찾습니다.android studio를 사용하여 파일에 데이터를 쓰는 방법
FloatingActionButton fab;
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener( new View.OnClickListener(){
@Override
public void onClick(View view){
final Thread t = new Thread() {
File openfilename= new File(Environment.getExternalStorageDirectory(),"myfile.txt");
FileOutputStream f;
@Override
public void run() {
try {
while (!isInterrupted()) {
Thread.sleep(500);
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView tv;
tv=(TextView)findViewById(R.id.tv);
tv.setText(" x="+Float.toString(xx)+" y="+Float.toString(yy)+" z="+Float.toString((zz))+" Puissance="+Float.toString(magneticStrenght));
try {
FileOutputStream f =new FileOutputStream(openfilename);
PrintWriter pw=new PrintWriter(f);
pw.append("\n x="+Float.toString(xx)+" y="+Float.toString(yy)+" z="+Float.toString((zz))+" Puissance="+Float.toString(magneticStrenght));
//close the file
pw.flush();
pw.close();
f.close();
} catch (java.io.IOException e) {
//do something if an IOException occurs.
Log.e("Exception", "File write failed: " + e.toString());
}
}
});
}
} catch (InterruptedException e) {
}
}
};
t.start();
제 코드에 어떤 문제가 있는지보십시오.
사실 나는 500ms마다 magnetomere의 값을 저장하려고합니다. 그래서 나는 루프를 가지고 있습니다. – dou