用properties写的一个程序运行次数计数的程序代码,超过次数提示注册。

       软件在开始免费使用,在使用次数已到是,将无法运行,并提示客户去注册。

package Properties;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class PropertiesTest {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		countApp();
	}

	public static void countApp() throws IOException {
		File file = new File("count.properties");//配置文件
		String key = "count";
		Properties pro = new Properties();
		if(!file.exists()){
			file.createNewFile();
		}
		FileInputStream fis = new FileInputStream(file);
		pro.load(fis);
		
		String value = pro.getProperty(key);
		
		int count = 0;
		if(value != null){
			count = Integer.parseInt(value);
			System.out.println("免费试用5次,已试用 "+count+" 次,剩余"+(5-count)+" 次");
			if(count >= 5){
				throw new RuntimeException("使用次数已到,请注册!");
			}
		}
		count++;
		pro.setProperty(key, String.valueOf(count));
		
		FileOutputStream fos = new FileOutputStream(file);
		pro.store(fos, key);
		fos.close();
		fis.close();
		
		
	}

	
}


 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值