gpt4 book ai didi

java - 安排一个java程序

转载 作者:行者123 更新时间:2023-12-03 23:03:07 24 4
gpt4 key购买 nike

我写了一个 java 套接字服务器程序,它连续监听一个端口。它为传入的数据创建一个新的文本文件,但我想每 30 分钟创建一个新的文本文件。

有人可以帮我安排它每 30 分钟运行一次吗?

谢谢。

@paul:我有以下代码:

import java.io.*;
import java.net.*;
import java.util.*;
import java.text.*;
import java.util.Timer;
import java.util.TimerTask;

public class DateServer extends Thread {

static public String str;

public static void main(String args[]) {

String pattern = "yyyyMMdd-hhmm";
SimpleDateFormat format = new SimpleDateFormat (pattern);
str = format.format(new Date());
int delay = 0;
int period = 180000;
Timer timer = new Timer();

ServerSocket echoServer = null;
String line = null;
DataInputStream is;
PrintStream os;
Socket clientSocket = null;

try {
echoServer = new ServerSocket(3000);
}
catch (IOException e) {
System.out.println(e);
}

try {
clientSocket = echoServer.accept();
is = new DataInputStream(clientSocket.getInputStream());
os = new PrintStream(clientSocket.getOutputStream());

while (true) {
line = is.readLine();
os.println("From server: "+ line);
System.out.println(line);

timer.scheduleAtFixedRate(new TimerTask() {
public void run(){
try{

FileWriter fstream = new FileWriter("C://" +str+".txt",true);
BufferedWriter out = new BufferedWriter(fstream);
out.write(line);
out.newLine();
out.flush();
out.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}

}
}}, delay, period);
}
catch (IOException e) {
System.out.println(e);
}
}
}
  1. timer.scheduleAtFixedRate(new TimerTask() 这一行,它给我以下错误:

    [no suitable method found for scheduleAtFixedRate() method java.util.Timer.scheduleAtFixedRate(java.util.TimerTask,java.util.Date,long) is not applicable (actual and formal argument lists differ in length) method java.util.Timer.scheduleAtFixedRate(java.util.TimerTask,long,long) is not applicable (actual and formal argument lists differ in length)]

  2. line = is.readLine(); 它给我以下错误:

    [cannot assign a value to final variable line].

我是java新手。对于糟糕的缩进,我深表歉意。请帮助我。

最佳答案

您的服务器只需要创建一个定时间隔,每三十分钟触发一次并创建文件。 See here for an example , Java docsanother example .

下面是代码片段,其中包含一些适用于您的情况的模组:

int delay = 0;   // delay for - no delay
int period = 1800000; // repeat every 1.8 mil milliseconds = 30 minutes
Timer timer = new Timer();

timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
// Create file here
}
}, delay, period);

并修复了代码:

import java.io.*;
import java.net.*;
import java.util.*;
import java.text.*;
import java.util.Timer;
import java.util.TimerTask;

public class DateServer extends Thread {
public static void main(String args[]) {
new Runner().go();
}
}

class Runner {
public static LinkedList<String> data = new LinkedList<String>();

public void go() {
ServerSocket echoServer = null;

MyTimerTask timerTask = new MyTimerTask();
new Timer().scheduleAtFixedRate(timerTask, 0, 2000);

try {
echoServer = new ServerSocket(3000);
}
catch (IOException e) {
System.out.println(e);
}

try {
Socket clientSocket = echoServer.accept();
DataInputStream is = new DataInputStream(clientSocket.getInputStream());
PrintStream os = new PrintStream(clientSocket.getOutputStream());

while (true) {
String line = is.readLine();
data.add(line);
os.println("From server: "+ line);
System.out.println(line);
}
}
catch (IOException e) {
System.out.println(e);
}
}
}

class MyTimerTask extends TimerTask {
public void run() {
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd-hhmm");
String line = null;
System.out.print(".");
try {
String str = format.format(new Date());
FileWriter fstream = new FileWriter("C://" +str+".txt",true);
BufferedWriter out = new BufferedWriter(fstream);
while (Runner.data.size() > 0) out.write(Runner.data.getLast());
out.newLine();
out.flush();
out.close();
} catch (Exception e) {//Catch exception if any
System.err.println("Error: " + e.getMessage() + e.getStackTrace()[0].toString());
}
}
}

关于java - 安排一个java程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7637030/

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