利用java实现windows关机

来源:互联网 发布:mysql读写分离 效率 编辑:程序博客网 时间:2024/05/29 09:20
/**
 * @author leaf
 * java关机程序
 */
package test;
import java.io.IOException;
import java.util.Scanner;
public class ShutDown {
    //关机定时
    public static final String time="60";
    //关机方法
    public void shutdown(){
        try {
            System.out.println("你确定关机吗?  y/n");
            Scanner sc=new Scanner(System.in);
            String s=sc.nextLine();
            if(s.equalsIgnoreCase("y")){
                //关机
                Runtime.getRuntime().exec("shutdown /s /t "+time);
            }
            System.out.println("输入'q'取消关机");
            s=sc.nextLine();
            if(s.equalsIgnoreCase("q")){
                //取消关机
                Runtime.getRuntime().exec("shutdown /a");
            }                        
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main(String args[]){
        new ShutDown().shutdown();}

}

0 0