【小程序】使用Properties配置文件限制程序使用次数

/*

* 小程序:使用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");

//关闭资源

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容