package io.properties;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import javax.management.RuntimeErrorException;
/*
* 定义功能 :获取一个应用程序运行的次数 如果超过5次 给出使用次数已到请注册的提示 并不要在运行程序
* 思路:
* 1 应该有计数器
* 每次程序启动都需要计数一次,并且是在原有的次数上进行计数
* 2 计数器就是一个变量 突然冒出一个想法 程序启动时进行记数计数器必须存在内存中并进行计算
* 可是程序一结束 计数器消失了 那么再次启动该程序 计数器又被重新初始化了
* 而我们需要多次启动同一个应用程序 使用的是同一个计数器
* 这就需要计数器的生命周期变长 从内存存储到硬盘文件中
*
* 3 如何使用这个计数器呢?
* 首先 程序启动时 应该先读取这个用于记录计数器信息的配置文件
* 获取上次计数次数 并进行试用次数的判断
* 其次对该次数惊醒自增 并将自增后的次数重新存储到配置文件中
*
* 4 文件中的信心该如很进行存储并体现
* 直接存储 次数值可以,但是不明确该数据的含义 所以起名字就变得很重要
* 这就有了名字和值的对应 所以可以使用键值对
* 可以 映射关系map集合搞定 又需要读取硬盘上的数据 所以map+io搞定
*/
public class PripertiesTest {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
getAppCount();
}
public static void getAppCount() throws IOException {
//将所需的配置文件封装成File对象
File confile=new File("count.ini");
if(!(confile.exists())) {
confile.createNewFile();
}
FileInputStream fis=new FileInputStream(confile);
Properties prop=new Properties();
prop.load(fis);
//从集合中通过键获取次数
String value=prop.getProperty("time");
int count=0;//定义计数器
if(value!=null) {
count=Integer.parseInt(value);
if(count>=5) {
//System.out.println("使用次数已到 请注册");
throw new RuntimeException("geiqian");
}
}
count++;
//将改变后的次数重新存储到集合中
prop.setProperty("time", count+"");
FileOutputStream fos=new FileOutputStream(confile);
prop.store(fos,"");
fos.close();
fis.close();
}
}
Java Properties 练习 计数器
最新推荐文章于 2021-12-27 21:58:03 发布