2013-06-18 37 views
0

该应用程序会记录传感器数据并将数据写入.txt文件中并存入手机SD卡。无法将最后一行写入.txt文件 - Android

在数据收集过程中,可以随时按停止按钮停止写入。

一旦停止按钮被按下,我想追加“文件结束”,然后关闭它。

但我从来没有看到在最后出现的字符串。哪里出错?

停止按钮部分:

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

     } 
+0

你如何验证该文件不包含最后一行?是否可能需要在行尾添加\ n? – njzk2

+0

@ njzk2我通过查找字符串“End Of File”来验证它是否包含最后一行。 txt文件中根本没有这样的字符串... –

+0

你可以发布验证码,只是为了安全起见吗? – njzk2

回答

2

stopSaving,冲洗和关闭作家开始:

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

使底层OutputStream关闭之前确保一切都将提交。

+0

对不起,但我实际上有这2条语句在我的代码中。我忘了粘贴它们了。它仍然不起作用。 :( –

0

让你的类中的方法lastline()写的最后一行,然后从onClick方法中调用这个函数。

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

请问有什么区别吗? –

+0

它应该......尝试一下 – jaisonDavis

+0

在这样一个大项目中修改代码是非常昂贵和风险的。在我去盲目修改之前,你能否说明理由? –