我正在Android中开发,我尝试通过.csv
将Android手机的传感器值记录到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
是否正确?
如何改善Android中BufferedWriter
的效果?
答案 0 :(得分:0)
是的,这是一种正确的行为,对于每次写入操作,您必须创建一个新的FileWriter
并关闭它。但是,从API 19开始,为了简单起见,您可以使用try-with-resources
执行此操作:
try (BufferedWriter bw = new BufferedWriter(new FileWriter(FILENAME))) {
bw.write(data);
// no need to close it.
//bw.close();
} catch (IOException e) {
e.printStackTrace();
}