从零用java写程序——自动关机程序的实现

一、让我们先来康康自动关机程序的效果

 1.1  下面是程序代码

public class demo1 {
    public static void main(String[] args) throws IOException {
        Runtime.getRuntime().exec("shutdown -s -t 3600");
    }
}

1.2  相应代码的解释

①  Runtime.getRuntime()   用Runtime类的getRuntime方法来获取系统当前的时间

②  RunTime.getRuntime().exec()   则可执行任何dos命令   这里执行的是定时自动关机命令

③  3600  的单位是秒

特别提醒

④  由于使用RunTime.getRuntime().exec()会提示异常,在这里我们将它抛出即可

二、不用担心真的自动关机了怎么办,因为还可以取消~@(。・o・)@

 

 2.1  老规矩,上代码

public class demo2 {
    public static void main(String[] args) throws IOException {
        Runtime.getRuntime().exec("shutdown -a");
    }
}

 2.3  这里就不需要解释了吧?同上

是不是很有意思|ω・),关注我一起进步吧!!o(*▽*)q

猜你喜欢

转载自blog.csdn.net/weixin_53353693/article/details/118737548