java中如何实现end退出程序
时间: 2024-03-02 12:50:29 浏览: 35
在Java中,可以使用System.exit(int status)方法来结束程序的运行。该方法接受一个整数参数作为程序的退出状态码,通常情况下,0表示正常退出,非0表示异常退出。例如,可以使用以下代码来结束程序的运行:
```
System.exit(0); // 正常退出程序
```
```
System.exit(1); // 异常退出程序
```
需要注意的是,使用System.exit方法会直接终止程序的运行,因此应该谨慎使用,避免在程序中过度使用该方法。
相关问题
java编写程序,程序运行时输出上次运行的开始时间和结束时间,然后用户按任意键退出程序。用property 方法
可以使用Java中的Properties类来实现这个功能。
首先,在程序开始时获取上次运行的结束时间,可以在程序结束时将结束时间存储在properties文件中,然后在程序开始时读取这个文件获取上次结束时间。
接下来,在程序结束时,将当前时间存储在properties文件中作为下次运行的开始时间。
最后,让用户按任意键退出程序即可。
以下是示例代码:
```java
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;
import java.util.Scanner;
public class Program {
private static final String PROPERTIES_FILE_NAME = "config.properties";
private static final String LAST_END_TIME_PROPERTY_NAME = "lastEndTime";
public static void main(String[] args) throws IOException {
// 读取上次运行的结束时间
Properties properties = new Properties();
try (FileInputStream in = new FileInputStream(PROPERTIES_FILE_NAME)) {
properties.load(in);
}
String lastEndTimeString = properties.getProperty(LAST_END_TIME_PROPERTY_NAME);
Date lastEndTime = lastEndTimeString != null ? new Date(Long.parseLong(lastEndTimeString)) : null;
// 输出上次运行的开始时间和结束时间
if (lastEndTime != null) {
System.out.println("上次运行开始时间:" + lastEndTime);
System.out.println("上次运行结束时间:" + new Date());
} else {
System.out.println("这是第一次运行本程序。");
}
// 存储本次运行的开始时间
properties.setProperty(LAST_END_TIME_PROPERTY_NAME, Long.toString(System.currentTimeMillis()));
try (FileOutputStream out = new FileOutputStream(PROPERTIES_FILE_NAME)) {
properties.store(out, "本文件存储本程序的配置信息。");
}
// 等待用户按任意键退出程序
System.out.println("请按任意键退出程序。");
new Scanner(System.in).nextLine();
}
}
```
在这个示例代码中,程序会在运行时读取名为`config.properties`的配置文件,如果文件中存在名为`lastEndTime`的属性,则认为这是上次运行的结束时间。程序会输出上次运行的开始时间和结束时间,并将本次运行的开始时间存储在`config.properties`文件中。最后,程序会等待用户按任意键退出。
java编写程序,程序运行时输出上次运行的开始时间和结束时间,然后用户按任意键退出程序。用property 方法来编写
下面是使用 Property 方法编写的 Java 程序,可以输出上次运行的开始时间和结束时间,并等待用户按任意键退出程序:
```java
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.Scanner;
public class LastRunTime {
public static void main(String[] args) throws IOException, InterruptedException {
Properties props = new Properties();
FileInputStream in = new FileInputStream("lastruntime.properties");
props.load(in);
in.close();
String startTime = props.getProperty("start_time");
String endTime = props.getProperty("end_time");
if (startTime != null && endTime != null) {
System.out.println("上次运行时间:" + startTime + " - " + endTime);
}
String newStartTime = getCurrentTime();
System.out.println("本次运行开始时间:" + newStartTime);
Scanner scanner = new Scanner(System.in);
System.out.println("按任意键退出程序。");
scanner.nextLine();
String newEndTime = getCurrentTime();
System.out.println("本次运行结束时间:" + newEndTime);
props.setProperty("start_time", newStartTime);
props.setProperty("end_time", newEndTime);
FileOutputStream out = new FileOutputStream("lastruntime.properties");
props.store(out, "Last run time");
out.close();
}
private static String getCurrentTime() {
return new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date());
}
}
```
该程序使用 Properties 类来读取和写入一个名为 lastruntime.properties 的文件,该文件保存了上次运行的开始时间和结束时间。当程序运行时,它会首先从该文件中读取上次运行的时间,如果存在则输出。然后程序会输出本次运行的开始时间,并等待用户按任意键退出程序。当用户按下任意键后,程序会输出本次运行的结束时间,并把本次运行的开始时间和结束时间保存到 lastruntime.properties 文件中。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)