Sibbs Gambling
Sibbs Gambling

Reputation: 20345

Cannot write the last line into the .txt file - Android

The app records the sensor data and write the data into a .txt file into the phone SD card.

During the data collection process, one may press the stop button anytime to stop writing.

Once the stop button is pressed, I wish to append "End Of File" and THEN close it.

But I never see the string of words appearing at the end. Where goes wrong??

Stop Button Part:

// 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();
    }
});

File Writing Part:

    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();
            }

        }

Upvotes: 0

Views: 208

Answers (2)

jaisonDavis
jaisonDavis

Reputation: 1427

Make a method lastline() inside your class to write the last line and then call this function from inside the onClick method.

public void lastline()
{
     myPrintWriter.write("End Of File");
     this.clearStampNumber();
     this.stopSaving();
}

Upvotes: 0

njzk2
njzk2

Reputation: 39386

in stopSaving, start by flushing and closing the writer :

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

to make sure everything is commited before the underlying outputStream is closed.

Upvotes: 2

Related Questions