2013-06-18 23 views
0

該應用程序記錄傳感器數據並將數據寫入到.txt文件中並存入手機SD卡。關閉文件時將不完整的行寫入txt文件 - Android

在數據收集過程中,可以隨時按停止按鈕停止書寫。

相關的寫作部分如下:

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; 

    boolean accelerationWanted; 
    boolean rotationRateWanted; 
    boolean magneticFieldWanted; 

    // 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() { 

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

    } 

} 

在我所說的序列SAVEDATA()和stopSaving()不管了,最後一行總是不完全的。對,我在主要活動中執行以下操作:

dataCollector.saveData(); 
dataCollector.stopSaving(); 

我先保存數據,然後停止保存。爲什麼最後一行仍然不完整?

任何想法如何解決這個問題?要麼完成最後一行或放棄它是好的。

在此先感謝!

回答

0

嘗試關閉之前,使用沖洗:

public void stopSaving() { 

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

} 
+0

不,每次都不行。 –

+0

刷新功能的描述:「確保所有待處理數據發送到目標」。我認爲這樣可以解決這個問題:( –

+0

起初我嘗試過3次,但一切正常,但第4次失敗... –

0

關閉它之前,你應該刷新你的作家的對象..

象下面這樣:

myOutWriter.flush(); 
    myOutWriter.close(); 

fOut.flush(); 
    fOut.close();