原来写着玩的,你可以看看,应该还有可以改进的地方。
[CODE]
package com.test;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
/**
*
* use thread to open the file at one time
*
*
*/
public class TextRuntime {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
RunPro rp = new RunPro();
rp.setOpenType(2);// by second
rp.setOpenTime(3);// open the file after 3s
rp.setOpenFileName("d:/names.txt");// file name
rp.start();// start the thread
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class RunPro extends Thread {
private String openFileName = "";// the open file's name
private int openTime = 0;// how long do you want to open the file
private int openType = 2;// minute?second?hour?
public String getOpenFileName() {
return openFileName;
}
public void setOpenFileName(String openFileName) {
this.openFileName = openFileName;
}
public int getOpenTime() {
return openTime;
}
public void setOpenTime(int openTime) {
this.openTime = openTime;
}
public int getOpenType() {
return openType;
}
public void setOpenType(int openType) {
if (openType == 0) {
openType = Calendar.HOUR;
}
if (openType == 1) {
openType = Calendar.MINUTE;
}
if (openType == 2) {
openType = Calendar.SECOND;
}
this.openType = openType;
}
public void run() {
Calendar startNow = Calendar.getInstance();// get the system time
startNow.add(getOpenType(), getOpenTime());// config
Date startTime = startNow.getTime();
while (true) {
// compare two time whether they are equal.
Calendar now = Calendar.getInstance();
Date nowTime = now.getTime();
// 通过线程一秒比较一次二个时间始终存在着时间差,所以二个时间无法相等,只好
// 通过先把二个时间转化成字符串来进行比较了。
if (startTime.toString().equals(nowTime.toString())) {
String proName = getOpenFileName();
try {
Runtime.getRuntime().exec("cmd /c start " + proName);// run
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.exit(1);
}
try {
sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
[/CODE]