gpt4 book ai didi

java - System.currentTimeMillis() 行为错误

转载 作者:行者123 更新时间:2023-12-01 10:41:50 24 4
gpt4 key购买 nike

我正在创建一个文本文件,其中的时间戳源自 System.currentTimeMillis() 的结果。我的算法是这样的:

  1. 创建文件并记录创建时间戳
  2. 每次按下按钮时保存时间戳
  3. 从按钮按下时间戳中减去文件创建时间戳
  4. 将结果写入文件

出于某种奇怪的原因,文件创建的时间戳通常比按钮按下的时间戳更大(更年轻、更近),而按钮按下的时间戳总是在文件创建后发生。这会导致第 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());
}

同样,问题是 AudioRecorder.java 中的 RecordStartTime 通常大于 MainActivity 中 recordNote 的输入参数“timeStamp”。例如,在最近的调试 session 中,recordingStartTime =1,450,573,093,044,而 timeStamp=1,450,565,187,318。

什么可能导致这种看似不可能的行为?

最佳答案

问题出在这一行:

myFile.createNewFile();

此方法的 JavaDoc 说

Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist

因此,每次您尝试开始新的记录时,您实际上都在重复使用同一个文件(带有很久以前的时间戳),但记录了新的 recordStartTime。

如果您想先删除旧文件,您必须编写

myFile.delete();
myFile.createNewFile();

关于java - System.currentTimeMillis() 行为错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34378191/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com