如何用JAVA语言编写每隔10秒钟向TXT文件中写入当前时间
2个回答
展开全部
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("异常");
}
}
}
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("异常");
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询