写入文本文件

时间:2017-04-14 16:55:17

标签: java android arraylist

我有两个由Android加速计传感器生成的方程式。我称为 totalAccelerate 的第一个等式代表 valueAfterMovingAverage ,第二个等式叫做 FrontalSpeed 。目标是将两个值(由空格分隔)一次写入我的文本文件。这是我找到的代码,它只编写了第一个quation值。 如何将第二个方程值与第一个值一起写? 结果必须是这样的:

10    7
8     9
7.5   6
 .    .
 .    .

这是我尝试的代码:

double TotalAccelerate;
double FrontalSpeed;
ArrayList<Double> listPeaks;
ArrayList<Double>listOfFrontal;


@Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listPeaks = new ArrayList<Double>();
    listOfFrontal = new ArrayList<Double>();
}//End OnCreate()

 @Override
public final void onSensorChanged(SensorEvent event) {
    // The light sensor returns a single value.
    // Many sensors return 3 values, one for each axis.
    if (isListening) {
        double xx = event.values[0];
        double yy = event.values[1];
        double zz = event.values[2];
        TotalAccelerate = Math.round(Math.sqrt(Math.pow(xx, 2)
                + Math.pow(yy, 2)
                + Math.pow(zz, 2)));
        Log.i(DEBUG, "Accelerometer = " + TotalAccelerate);


        FrontalSpeed =  Math.round(Math.sqrt(Math.pow(xx, 2)
                + Math.pow(zz, 2))); //Second equation


          Log.i(DEBUG, "list values " + listPeaks);

        MovingAverage ma = new MovingAverage(2);
        ma.newNum(TotalAccelerate);
        valueAfterMovingAverage= (float) ma.getAvg();
        sensorText.setText(String.valueOf(valueAfterMovingAverage));
        Log.i(DEBUG, "Moving avg:  " + valueAfterMovingAverage);
        listPeaks.add((double) valueAfterMovingAverage);
        listOfFrontal.add(FrontalSpeed); //I think this is wrong, I should add the values to the same list above 
and concatenate with space. 
     }

}

 OnStore = (ToggleButton) findViewById(R.id.onStore);
    OnStore.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            if (OnStore.isChecked()) {
                //
                // set listening flag to true
                //
                isListening = true;


            } else if (!OnStore.isChecked()) { //
                // set listening flag to false
                //
                isListening = false;

                try {
                    for (double valueAfterMovingAverage : listPeaks) {
                        String space = "\n";
                        byte[] convert = space.getBytes();
                        fileOutputStream.write(convert);
                        String finalData;
                        String speedSignal = String.valueOf(FrontalSpeed);

                        finalData = String.valueOf(valueAfterMovingAverage);

                        fileOutputStream.write(Integer.parseInt(finalData.getBytes()+" " + speedSignal)); //my attempt. 
                           Log.i(DEBUG, "ans: " + finalData);
                       // fileOutputStream.write(Integer.parseInt(speedSignal));
                      }
                    fileOutputStream.flush();
                    fileOutputStream.close();
                    Toast.makeText(getApplicationContext(), "Message saving", Toast.LENGTH_LONG).show();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Toast.makeText(getApplicationContext(), "Message Stopped.", Toast.LENGTH_LONG).show();
            }
        }
    });

0 个答案:

没有答案