0

该应用程序记录传感器数据并将数据写入 .txt 文件到手机 SD 卡中。

在数据采集过程中,可以随时按下停止键停止写入。

按下停止按钮后,我希望附加“文件结尾”然后关闭它。

但我从来没有看到最后出现的那一串单词。哪里出错了??

停止按钮部分:

// stop button
stopButton = (Button) findViewById(R.id.button6);
stopButton.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {

        startFlag = false;

        // record down the step count both in file and UI
        listAdapter.add(txtName.getText() + ".txt: " + String.valueOf(stepCount));
        dataCollector.myPrintWriter.write("End Of File");

        dataCollector.clearStampNumber();
        dataCollector.stopSaving();
    }
});

文件写入部分:

    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;

    import android.widget.CheckBox;
    import android.widget.EditText;

    public class DataCollector {

        File myFile;
        FileOutputStream fOut;
        OutputStreamWriter myOutWriter;
        BufferedWriter myBufferedWriter;
        PrintWriter myPrintWriter;

        private boolean isStamped;
        private int timeStampNo;

        // constructor
        public DataCollector() {

            isStamped = false;
            timeStampNo = 0;

            accelerationWanted = false;
            rotationRateWanted = false;
            magneticFieldWanted = false;
        }

        public void setStamp() {

            isStamped = true;
            timeStampNo++;
        }

        public void setFilePath(EditText txtName) {

            myFile = new File("/sdcard/ResearchData/" + txtName.getText() + ".txt");

            try {
                myFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                fOut = new FileOutputStream(myFile);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

            myOutWriter = new OutputStreamWriter(fOut);
            myBufferedWriter = new BufferedWriter(myOutWriter);
            myPrintWriter = new PrintWriter(myBufferedWriter);

        }

        public void saveData(double[] acceleration, double[] rotationRate, double[] magneticField, long startTime, long currentTime) {

                myPrintWriter.write(currentTime - startTime + " " + acceleration[0] + " " + acceleration[1] + " " + acceleration[2] + " " + rotationRate[0] + " " + rotationRate[1] + " " + rotationRate[2] + " " + magneticField[0] + " " + magneticField[1] + " " + magneticField[2] + "\n");
        }

        public void stopSaving() {

            myPrintWriter.flush();
            myPrintWriter.close();

            try {
                myOutWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (NullPointerException e) {
                e.printStackTrace();
            }
            try {
                fOut.close();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (NullPointerException e) {
                e.printStackTrace();
            }

        }
4

2 回答 2

2

在中stopSaving,首先刷新并关闭 writer:

myPrintWriter.flush();
myPrintWriter.close();

确保在底层 outputStream 关​​闭之前提交所有内容。

于 2013-06-18T08:24:51.353 回答
0

在你的类中创建一个方法lastline()来编写最后一行,然后从方法内部调用这个函数onClick

public void lastline()
{
     myPrintWriter.write("End Of File");
     this.clearStampNumber();
     this.stopSaving();
}
于 2013-06-18T08:18:47.177 回答