2015-12-20 30 views
0

我正在創建一個帶有從System.currentTimeMillis()結果派生的時間戳的文本文件。我的算法是這樣的:System.currentTimeMillis()錯誤行爲

  1. 創建文件和創造的紀錄時間戳
  2. 每按下一個按鈕
  3. 減去文件創建時間戳記後的按鍵時間戳
  4. 結果寫入文件時
  5. 保存時間戳

出於某種奇怪的原因,文件創建的時間戳通常比按鈕按下的時間戳更大(更年輕,更近期),這通常會在創建文件後發生。這導致從步驟3返回負值。

這是什麼造成的?

FileCreationMenu.java

public class FileCreationMenu extends Fragment { 
      public Button toggleRecordingButton; 
      @Override 
      public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { 
       View v = inflater.inflate(R.layout.menu_fragment_calibrator, container, false); 
       toggleRecordingButton = (Button) v.findViewById(R.id.recordAudioToggle); 
       toggleRecordingButton.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         recordAudio = !recordAudio; //toggle audio recording on/off 
         if (recordAudio==true){ 
          AudioRecorder.createFile(System.currentTimeMillis()); //generate file for new track if record is togged ON 
         } 
        } 
       }); 

AudioRecorder.java

public static void createFile(long time) { //create file and record creation timeStamp 
     recordingStartTime = time; 
     myFile = new File(Environment.getExternalStorageDirectory(), "file.txt"); //save to external storage 
     try { 
      myFile.createNewFile(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    public static void recordNote(long timeStamp){ //record timeStamps of notes 
     String playedNote = (timeStamp+ "\n"); 
     try { 
      fos = new FileOutputStream(myFile, true); 
      fos.write(playedNote.getBytes()); 
      fos.flush(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    public static void playTrack(String fileName){ //playback Notes 
    try { 
     FileInputStream fis = new FileInputStream(myFile.getPath()); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(fis)); 
     String line; 
     try { 
      int i =0; 
      while ((line = reader.readLine()) != null) { //read each line of input file 
       historicalTime[i] = Long.parseLong(line); //store time current line's note was played 
       if (i==0){ 
        timeStream[i] = (Long.parseLong(line) - recordingStartTime); //if first note, calculate wait based on file creation time 
       } 
       else{ 
        timeStream[i] = (Long.parseLong(line) - historicalTime[i-1]); //otherwise, calculate wait as amount of time note was played after most recent preceding note 
       } 
       i++; 
      } 
     } catch (IOException e){ 
      e.printStackTrace(); 
     } 
     for (int i=0; i<noteStream.length; i++){ 
      try { 
       AudioRecorder.class.wait(timeStream[i]); //wait 
       //Play Note 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
     } 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 
} 

MainActivity.java

... 
public static void noteDetected(){ 
     if (FileCreationMenu.recordAudio == true){ 
      AudioRecorder.recordNote(System.currentTimeMillis()); 
     } 

銀ain,問題在於AudioRecorder.java中的recordingStartTime通常大於輸入參數「timeStamp」來記錄MainActivity中的筆記。例如,在最近的調試會話recordingStartTime = 1,450,573,093,044而timeStamp = 1,450,565,187,318。

什麼可能導致這種看似不可能的行爲?

+0

你在哪裏調用''noteDetected? – shhp

+0

那裏有很多靜態方法和函數。你有沒有檢查過recordingStartTime只設置一次,只有在該按鈕內?你可以分享寫入文件的一部分? – Jan

+0

@shhp noteDetected是MainActivity中的一個獨立方法,發生在onCreate()之外。還有一大堆其他因素是否檢測到一個音符,所以我沒有打擾在這裏列出所有的音符。我相信這是我的問題中該類代碼中唯一相關的部分。 – Cody

回答

1

的問題是這一行:

myFile.createNewFile(); 

此方法的JavaDoc的說

原子地創建由此抽象路徑名命名一個新的空文件,當且僅當與文件這個名字還不存在

所以每次你嘗試開始一個新的錄音你是一個重複使用同一個文件(使用很久以前的時間戳),但記錄一個新的recordStartTime。

如果你想刪除舊文件,首先你必須寫

myFile.delete(); 
myFile.createNewFile(); 
+0

這很有道理!感謝您的回答,我正在測試解決方案。如果它有效,你會得到支票。 – Cody

相關問題