程序运行次数练习

来源:互联网 发布:爱名网域名过户 编辑:程序博客网 时间:2024/05/16 15:49
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class JiShu {

public static void main(String[] args) throws IOException {

boolean b = checkCount();
if (b)
run();

}

private static boolean checkCount() throws IOException {
boolean isrun = true;
File configFile = new File("count.properties");
if (!configFile.exists()) {
configFile.createNewFile();

int count = 0; // 记录每次存储的次数
Properties prop = new Properties(); // 用于储存配置文件中的文件
FileInputStream fis = new FileInputStream(configFile);
// 把流中的数据加载到集合中
prop.load(fis);
// 获取键对应的次数
String key = "count";
// 获取键对应的次数
String value = prop.getProperty("count");
if (value != null) {
count = Integer.parseInt(value);
if (count >=5){
System.out.println("使用次数已到,请充钱!!");
isrun = false;
}
}
count++; // 使用的次数加一
prop.setProperty("count", Integer.toString(count));
// 将集合中的数据存储到配置文件中去
FileOutputStream fos = new FileOutputStream(configFile);
prop.store(fos, "");
fos.close();
fis.close();
return isrun;

}
private static void run() {
System.out.println("软件运行了!");
}
}
0 0
原创粉丝点击