我试图让我的程序每 5 分钟运行一次。谁能告诉我如何实现这一目标?我对java相当陌生,这就是我到目前为止所拥有的。这是一个将进程打印到文本文件,然后调用该文本文件以显示其数据的程序。最好每 5 分钟运行一次。
import java.io.*;
import java.util.StringTokenizer;
public class getProcesses
{
private String GetProcessListData()
{
Process p;
Runtime runTime;
String process = null;
try {
System.out.println("Allow me to hack your system and see what you have open...");
//This will let us access windows in order to make this program possible
runTime = Runtime.getRuntime();
p = runTime.exec("tasklist");
InputStream inputStream = p.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String line = bufferedReader.readLine();
process = "&";
while (line != null) {
line = bufferedReader.readLine();
process += line + "&";
}
//As we learned in class, we have to close whatever we open
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
//The following output is a quote from Portal
System.out.println("System has been modified. Thankyou and Goodbye.");
} catch (IOException e) {
System.out.println("Uh oh, looks like we found an IOE");
e.printStackTrace();
}
return process;
}
private void showProcessData()
{
try {
String proc = GetProcessListData();
OutputStreamWriter outputStreamWriter =
new OutputStreamWriter(new FileOutputStream("ProcessList.txt"));
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
StringTokenizer st = new StringTokenizer(proc, "&");
while (st.hasMoreTokens()) {
bufferedWriter.write(st.nextToken());
bufferedWriter.newLine();
}
//This will close the outputStreams
bufferedWriter.close();
outputStreamWriter.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
@SuppressWarnings("resource")
public static void main(String[] args) throws IOException
{
getProcesses gpl = new getProcesses();
gpl.showProcessData();
BufferedReader in = new BufferedReader(new FileReader("ProcessList.txt"));
String line;
while((line = in.readLine()) != null)
{
System.out.println(line);
}
}
}
任何帮助表示赞赏,谢谢。