计划在Java中多次启动方法
我正在编写一个程序,在管理员指定的特定时间将文件上载到服务器,管理员输入多个值(小时、分钟) 例如:计划在Java中多次启动方法,java,scheduled-tasks,scheduling,reminders,Java,Scheduled Tasks,Scheduling,Reminders,我正在编写一个程序,在管理员指定的特定时间将文件上载到服务器,管理员输入多个值(小时、分钟) 例如: [Hours,Minutes]= [2,12] [ 2,15],[ 5,20 ] 我将这些值保存在CSV文件中 BufferedReader reader; try { reader = new BufferedReader(new FileReader("C:/Users/BACKENDPC1/Desktop/timer.csv"));
[Hours,Minutes]= [2,12] [ 2,15],[ 5,20 ]
我将这些值保存在CSV文件中
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader("C:/Users/BACKENDPC1/Desktop/timer.csv"));
String line = null;
while ((line = reader.readLine()) != null) {
lines.add(line);
}} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();}
//Get the Date corresponding to 11:01:00 pm today.
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH");
Format formatter = new SimpleDateFormat("m");
Format sec=new SimpleDateFormat("s");
/*heur=getList().get(i).substring(0, getList().get(i).indexOf(substr));
minute=getList().get(i).substring(getList().get(i).indexOf(substr) + substr.length());
System.out.println("Time selected is: "+heur+","+minute);*/
while (i<lines.size()) {
heur=lines.get(i).substring(0, lines.get(i).indexOf(substr));
minute=lines.get(i).substring(lines.get(i).indexOf(substr) + substr.length());
System.out.println(sdf.format(calendar.getTime()));
System.out.println(Integer.parseInt(formatter.format(new Date())));
if(Integer.parseInt(sdf.format(calendar.getTime()))==Integer.parseInt(heur)&&(Integer.parseInt(formatter.format(new Date()))==Integer.parseInt(minute))){
System.out.println(Integer.parseInt(heur)+"H"+ Integer.parseInt(minute));
calendar.set(Calendar.HOUR_OF_DAY,Integer.parseInt(heur) );
calendar.set(Calendar.MINUTE, Integer.parseInt(minute));
calendar.set(Calendar.SECOND, 0);
Date time = calendar.getTime();
timer = new Timer();
timer.schedule(new RemindTask(), time);
i++;
}}
i=1;
start();
/* timer = new Timer();
timer.schedule(new RemindTask(), seconds*1000);*/
}
class RemindTask extends TimerTask {
public void run() {
up.Uplaod();
long start = new Date().getTime();
long end=0;
int numIndexed=0;
boolean cond=true;
end = new Date().getTime();
cond=false;
// System.out.println("Indexing " + numIndexed + " files took "
// + (end - start) + " milliseconds");
timer.cancel();
//Terminate the timer thread
}
BufferedReader读取器;
试一试{
reader=新的BufferedReader(新的文件阅读器(“C:/Users/BACKENDPC1/Desktop/timer.csv”);
字符串行=null;
而((line=reader.readLine())!=null){
行。添加(行);
}}catch(filenotfounde异常){
//TODO自动生成的捕捉块
e、 printStackTrace();}
//获取对应于今天晚上11:01:00的日期。
日历=Calendar.getInstance();
SimpleDataFormat sdf=新的SimpleDataFormat(“HH”);
格式格式化程序=新的SimpleDataFormat(“m”);
格式sec=新的SimpleDataFormat(“s”);
/*heur=getList().get(i).substring(0,getList().get(i).indexOf(substr));
minute=getList().get(i).substring(getList().get(i).indexOf(substr)+substr.length());
System.out.println(“所选时间为:“+heur+”,“+min”)*/
while(i不要重新发明轮子。试试。数字格式异常:对于输入字符串:“小时”字符串“小时”
,毫不奇怪,无法解析为int
。请更改用于提取实际数字的逻辑。它看起来像是带有列名的csv标题。您可能应该跳过它。顺便说一句,从Java 7开始,有一种一行的方法来读取文件中的所有行:文件。readAllLines
。您可以按照以下操作:第行。删除(0)
并解决您的问题。谢谢我解决了问题,谢谢youMeiblorn,谢谢您的回答我尝试使用cron,但我得到了以下信息:您能帮助我吗