/*
* 小程序:使用Properties配置文件限制程序只能运行3次
*/
package com.michael.lin;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class Demo5 {
public static void main(String[] args) throws IOException{
//1.定位配置文件对象s
File file = new File("c:\\conf.properties");
//如果文件不存在,就新建
if(!file.exists()){
file.createNewFile();
}
//2.建立文件输入通道
//3.加载配置文件
Properties pro = new Properties();
pro.load(new FileInputStream(file));
//3.判断文件被读取的次数
int count = 0;
String value = pro.getProperty("count");
if(value!=null){
count=Integer.parseInt(value);
count++;
if(count==3){
System.out.println("您已经使用了3次,请购买正版!");
System.exit(0);
}else{
System.out.println("您已经使用了" + count + "次");
}
}
//设置新值
pro.setProperty("count", count+"");
//保存设置
pro.store(new FileOutputStream(file), "count config");
//关闭资源
}
}