gpt4 book ai didi

java - 定时器无法正常工作(Java)

转载 作者:行者123 更新时间:2023-12-01 11:18:57 27 4
gpt4 key购买 nike

我正在编写一个简单的 Java 代码,它使我能够将当前时间输出到单个文本文件中。我能够成功地将所有当前时间写入文本文件。然而,然后我尝试使用一个计时器,每 4 秒触发一个简单的任务(输出当前时间),计时器无法正常工作。这怎么可能?我该如何解决这个问题?谢谢!

代码:

      import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class testWriteTimeToFile extends javax.swing.JFrame {

public Writer writer=null;
public File file;
protected boolean isRunning=false;
public Timer timer = new Timer();

public testWriteTimeToFile() {
initComponents();
initTimer();
}

public void initTimer()
{
this.isRunning=true;
tryToGetUpdateTime();
}

public void tryToGetUpdateTime()
{
java.awt.EventQueue.invokeLater(new Runnable() {

SimpleDateFormat sdfMonth = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat sdfHour = new SimpleDateFormat("HH:mm:ss");

public void run() {
while (isRunning) {
TimerTask task = new TimerTask()
{
public void run()
{
try{
file= new File("c:/Users/user/Desktop/updateTime.txt");

if(!file.exists())
{
file.createNewFile();
}

FileWriter fileWriter = new FileWriter(file,true);
BufferedWriter bufferWritter = new BufferedWriter(fileWriter);

Date date = new Date();

bufferWritter.append(sdfMonth.format(date) + " " + sdfHour.format(date) + '\n');

bufferWritter.close();
}catch(IOException ex){
System.err.println("Error in Writer : " + ex);
}
}
};
timer.scheduleAtFixedRate(task,0,4000);
}
}
});
}

public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new testWriteTimeToFile().setVisible(true);
}
});
}
}

最佳答案

您需要改进您的编码。以下是您在代码中需要注意的事项

  • Java 命名约定

  • 程序中有无限循环

  • 最好从顶部导入包,而不是从底部导入包。代码
  • 不需要使用 swing 的东西。
  • 您需要学习如何使计时器工作。

尝试下面的代码,我为你重写了

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class testWriteTimeToFile {

public Writer writer = null;
public File file;
protected boolean isRunning = false;
public Timer timer = null;

public testWriteTimeToFile(int n) {
// initComponents();
initTimer();
timer = new Timer();

// run task every 4 seconds
timer.schedule(new Task(1,2,3), 0, n * 1000);
}

public void initTimer() {
this.isRunning = true;
// tryToGetUpdateTime();
}

class Task extends TimerTask {

private int a,b,c;
private double e,f,g;
private String h,i,j;

// take int
public Task (int a, int b, int c){

this.a = a;
this.b = b;
this.c = c;
}

// take double
public Task (double e, double f, double g){

this.e = e;
this.f = f;
this.g = g;
}

// take string
public Task (String h, String i, String j){

this.h = h;
this.i = i;
this.j = j;
}

@Override
public void run() {
final SimpleDateFormat sdfMonth = new SimpleDateFormat("dd/MM/yyyy");
final SimpleDateFormat sdfHour = new SimpleDateFormat("HH:mm:ss");
// only do it for 5 second
file = new File("c:/test/time.txt");

try {

FileWriter fileWriter = new FileWriter(file, true);
BufferedWriter bufferWritter = new BufferedWriter(fileWriter);

Date date = new Date();

bufferWritter.append(sdfMonth.format(date) + " "
+ sdfHour.format(date) + '\n');

bufferWritter.close();
} catch (IOException ex) {
System.err.println("Error in Writer : " + ex);
}

}

}

public static void main(String args[]) {

new testWriteTimeToFile(4);

}
}

关于java - 定时器无法正常工作(Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31482037/

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