用java实现PC限时关机

本文介绍了一个用Java实现的定时控制系统,该系统能够记录电脑的使用时间,并在达到预设的时间限制后自动关闭计算机。系统通过读取和写入文件来保存时间数据,利用Java Swing进行用户提示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载:http://hi.baidu.com/triceratops/blog

import java.io.*;
import javax.swing.JOptionPane;

import java.util.Calendar;
import java.io.Serializable;

public class TimeController{
private Timer timer;
private File f = new File("timer");
public void execute(int hour) throws IOException,ClassNotFoundException,InterruptedException{
FileInputStream fis;
ObjectInputStream ois;
while(true){
try{
fis = new FileInputStream(f);
ois = new ObjectInputStream(fis);
timer = (Timer)ois.readObject();
ois.close();
}
catch(FileNotFoundException ioe){
saveTimer(new Timer());
continue;
}
if(timer.isTimeExhausted() && timer.isTodayOpened()){
closePC();
JOptionPane.showMessageDialog(null,"今日开机时间已经满"+hour+"小时!","警告!",0);
break;
}
if(!timer.isTodayOpened()){
timer.setTimeExhausted(false);
timer.setDate();
}
timer.setStartTime();
Thread.sleep(10*60*1000);
timer.setCurrentTime();
timer.addEclipsedSeconds();
saveTimer(timer);
if(timer.isShutdownTime(hour)){
closePC();
timer.reset();
saveTimer(timer);
JOptionPane.showMessageDialog(null,"请注意:10分钟后将自动关机!","警告!",0);
break;
}
}
}

private void saveTimer(Timer timer) throws IOException{
FileOutputStream fos = new FileOutputStream(f);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(timer);
oos.close();
}

private void closePC() {
new Thread(new Runnable(){
public void run() {
try{
if(timer.getEclipsedSeconds() != 0)
Thread.sleep(10*60*1000);
Runtime.getRuntime().exec("shutdown -s");
}
catch(Exception e){
e.printStackTrace();
}
}
}).start();
}

public static void main(String[] args) throws Exception{
int hour = 3;
if(args.length != 0)
hour = Integer.parseInt(args[0]);
new TimeController().execute(hour);
}
}

=============================================

public class Timer implements Serializable{
transient private long startTime;
transient private long currentTime;
private long eclipsedSeconds;
private boolean timeExhausted;
private int date;

public Timer(){
setStartTime();
setCurrentTime();
setDate();
}

public void setStartTime(){
startTime = Calendar.getInstance().get(Calendar.HOUR_OF_DAY)*60*60+
Calendar.getInstance().get(Calendar.MINUTE)*60+
Calendar.getInstance().get(Calendar.SECOND);
}

public long getStartTime(){
return startTime;
}

public void setCurrentTime(){
currentTime = Calendar.getInstance().get(Calendar.HOUR_OF_DAY)*60*60+
Calendar.getInstance().get(Calendar.MINUTE)*60+
Calendar.getInstance().get(Calendar.SECOND);
}

public long getCurrentTime(){
return currentTime;
}

public void addEclipsedSeconds(){
eclipsedSeconds += currentTime - startTime;
}

public long getEclipsedSeconds(){
return eclipsedSeconds;
}

public void setDate(){
date = Calendar.getInstance().get(Calendar.DATE);
}

public int getDate(){
return date;
}

public void setTimeExhausted(boolean timeExhausted){
this.timeExhausted = timeExhausted;
}

public boolean isTimeExhausted(){
return timeExhausted;
}

public boolean isTodayOpened(){
return date == Calendar.getInstance().get(Calendar.DATE);
}

public boolean isShutdownTime(int hour){
if(eclipsedSeconds>=hour*60*60)
return true;
return false;
}

public void reset(){
eclipsedSeconds = 0;
setTimeExhausted(true);
}

public String toString(){
String eclipsedTime = "今日累计使用了:";
long temp = getEclipsedSeconds();
eclipsedTime += temp/(60*60) + "小时";
temp = temp%(60*60);
eclipsedTime += temp/60 + "分";
temp = temp%60;
eclipsedTime += temp + "秒";
return eclipsedTime;
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值