package com.yyq;
import java.io.*;
/*
* 用于记录应用程序的运行次数
* 如果使用次数已到,那么给出注册提示
*
* 很容易想到: 计数器
* 可是该计数器定义在程序中,随着程序的运行而在内存中存在,并进行自增
* 可是随着该应用程序的退出,该计数器也在内存中消失了
*
* 下次程序启动会先加载计数器的值并加1后再重新存储起来
* 所以要建立一个配置文件,用于记录该软件的使用次数
该配置文件使用键值对的形式,这样便于阅读数据
并操作数据
键值对数据是map集合
数据是以文件形式存储,使用IO技术
那么map+io = properties
配置文件可以实现应用程序数据的共享
*/
// 配置文件 : xml properties
import java.util.*;
public class PropertiesTest {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Properties prop = new Properties();
//在操作文件时,养成习惯,先把文件封装成对象
File file = new File("count.properties");
// 封装完文件 ,可以对文件进行判断
if(!file.exists()){
file.createNewFile();
}
FileInputStream fis = new FileInputStream(file);
prop.load(fis);
// 获取到值
String value = prop.getProperty("time");
int count = 0;
if(value!=null){
count = Integer.parseInt(value);
if(count>=5){
System.out.println("您好,使用次数已到");
}
}
count++;
prop.setProperty("time",count+"");
FileOutputStream fos = new FileOutputStream(file);
prop.store(fos, "");
fos.close();
fis.close();
}
}