IO流练习:记录程序运行的次数

【需求】:

记录程序运行的次数,当程序的运行次数达到上限(5次)后,禁止登陆。

思路:
由于计数器存在于程序内,随着程序的结束而结束;在重新运行程序时,计数器置为0;
所以这里采用一个文件来存储运行次数,第二次运行时,从文件夹里拿出上次运行的次数,进行加1后存储。

【代码】:

import java.io.*;
import java.util.*;
class  Count
{
	public static void main(String[] args) throws IOException
	{
		Properties pro=new Properties();
		File file=new File("d:\\WorkSpace\\IO流\\properties.txt");
		if(!file.exists()){
			file.createNewFile();
		}
		FileInputStream fis=new FileInputStream(file);
		pro.load(fis);
		
		String value=pro.getProperty("time");
		int num=0;
		if(value!=null){
			num=Integer.parseInt(value);
			if(num>=5){
			    System.out.println("试用次数已达上限,请充值后登陆");
				return ;
			}
		}
		num++;
		pro.setProperty("time",num+"");
		FileOutputStream fos=new FileOutputStream(file);
		pro.store(fos,"");
		fos.close();
		fis.close();
	}
}

【截图】: 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值