如何用JAVA语言编写每隔10秒钟向TXT文件中写入当前时间

如题所述

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.text.DateFormat;
import java.util.Date;

public class Test{
public static void main(String[] args) {
Thread t = new Thread(new WriteTime());
t.start();
}
}
class WriteTime implements Runnable{
public void run(){
try{
Writer fw = new FileWriter(new File("F:/a.txt"));
while(true){
Date date = new Date() ;
DateFormat time = DateFormat.getTimeInstance();
String s = time.format(date);
fw.write(s+" ");
fw.flush();
System.out.println(s);
Thread.sleep(10000);
}
}catch(Exception e){
System.out.println("异常");
}
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-11-21
用timer了
相似回答