Я работаю в Android и пытаюсь записать значение датчика телефона Android в файл .csv
через BufferedWriter
.
Код выглядит следующим образом:
//recordData
private float ACCELEROMETER_Gx = 0 , ACCELEROMETER_Gy = 0 , ACCELEROMETER_Gz = 0;
private float GRAVITY_X = 0 , GRAVITY_Y = 0 , GRAVITY_Z = 0;
private float GYROSCOPE_X = 0 , GYROSCOPE_Y = 0 , GYROSCOPE_Z = 0;
private float ACCELERATION_X = 0 , ACCELERATION_Y = 0 , ACCELERATION_Z = 0;
private float MAGNETIC_FIELD_X = 0 , MAGNETIC_FIELD_Y = 0 , MAGNETIC_FIELD_Z = 0;
private float PRESSURE = 0;
private float ROTATION_VECTOR_X = 0 , ROTATION_VECTOR_Y = 0 , ROTATION_VECTOR_Z = 0;
Когда датчик изменится, он запишет в файл .csv
.
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
ACCELEROMETER_Gx = event.values[0];
ACCELEROMETER_Gy = event.values[1];
ACCELEROMETER_Gz = event.values[2];
}else if (event.sensor.getType() == Sensor.TYPE_GRAVITY) {
GRAVITY_X = event.values[0];
GRAVITY_Y = event.values[1];
GRAVITY_Z = event.values[2];
}else if (event.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
GYROSCOPE_X = event.values[0];
GYROSCOPE_Y = event.values[1];
GYROSCOPE_Z = event.values[2];
}else if (event.sensor.getType() == Sensor.TYPE_LINEAR_ACCELERATION) {
ACCELERATION_X = event.values[0];
ACCELERATION_Y = event.values[1];
ACCELERATION_Z = event.values[2];
}else if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
MAGNETIC_FIELD_X = event.values[0];
MAGNETIC_FIELD_Y = event.values[1];
MAGNETIC_FIELD_Z = event.values[2];
}else if (event.sensor.getType() == Sensor.TYPE_PRESSURE) {
PRESSURE = event.values[0];
}else if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
ROTATION_VECTOR_X = event.values[0];
ROTATION_VECTOR_Y = event.values[1];
ROTATION_VECTOR_Z = event.values[2];
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
String SensorChangeTime = sdf.format(new Date(System.currentTimeMillis()));
String SensorValue =
SensorChangeTime + "," +
appendcomma(ACCELEROMETER_Gx) + appendcomma(ACCELEROMETER_Gy) + appendcomma(ACCELEROMETER_Gz) +
appendcomma(GRAVITY_X) + appendcomma(GRAVITY_Y) + appendcomma(GRAVITY_Z) +
appendcomma(GYROSCOPE_X) + appendcomma(GYROSCOPE_Y) + appendcomma(GYROSCOPE_Z) +
appendcomma(ACCELERATION_X) + appendcomma(ACCELERATION_Y) + appendcomma(ACCELERATION_Z) +
appendcomma(MAGNETIC_FIELD_X) + appendcomma(MAGNETIC_FIELD_Y) + appendcomma(MAGNETIC_FIELD_Z) +
appendcomma(PRESSURE) +
appendcomma(ROTATION_VECTOR_X) + appendcomma(ROTATION_VECTOR_Y) + appendcomma(ROTATION_VECTOR_Z) + '\n';
writeData(SensorValue);
}
private String appendcomma(float value){
return String.valueOf(value) + ",";
}
private void writeData(String Data){
try {
fw = new FileWriter(path+"/" + FileRecordName, true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(Data);
bw.close();
}catch (IOException e){
Log.d(TAG,"writeData---e = " + e.toString());
}
}
При изменении значения датчика он каждый раз записывает в файл .csv
следующий код:
fw = new FileWriter(path+"/" + FileRecordName, true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(Data);
bw.close();
Правильно или нет для создания new FileWriter
и coles
каждый раз?
Как улучшить производительность BufferedWriter
в Android?