JAVA之IO技术相关 如何设置软件的使用次数

2021/6/3 20:21:32

本文主要是介绍JAVA之IO技术相关 如何设置软件的使用次数,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

/*
 * 如何设置软件的使用次数?
 * 开发了一款软件,可以给用户进行试用,但是到了一定次数后,软件不能再被试用
 * 同时提醒用户付费。
 * 思路:
 * 计数呗
 * 问题出现,计数器只能存在于程序运行过程中,实际是保存在内存中的。
 * 那么一旦程序结束,这个计数器的值又恢复为了初始程序中设定的值。
 * 那么如何保存这个值,在下一次启动应用程序的时候,让其仍然存在啊
 * 思路:让这个值持久化,方法将其值保存在硬盘上的文件上。再每次运行
 * 程序之前,先读取这个配置文件;程序关闭之前存储信息到配置文件。
 * 当启动程序的时候,读取到的值如果超过我们设定给用户的次数,则终止
 * 程序启动,就OK啦。
 * 
 */
package ioTest.io3;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

public class PropertiesControlCountToAPP {
	public static void main(String[] args) {
		try {
			loadConfigCount();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void loadConfigCount() throws IOException
	{
		//配置文件信息
		File confile=new File("F:\\configure.ini");
		Properties pro= new Properties();
		//读信息也可以直接用load方法解决
		BufferedReader in=null;
		String line=null;
		int count=1;
		try {
			in=new BufferedReader(new FileReader(confile));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		while((line=in.readLine())!=null)
		{
			if(!line.contains("="))//如果读取到一行没有=号,说明不是键值对继续执行循环
				continue;
			String[] confPmr=line.split("=");
			//将配置文件读取到properties集合中存储
			pro.setProperty(confPmr[0],confPmr[1]);	
		}
		count=Integer.parseInt(pro.getProperty("count"));
		if(count>5)
		{
			System.out.println("软件使用次数已到了,给点辛苦费,可以继续使用哦");
			return;
		}
		System.out.println("您是第"+count+"次使用本软件!您还可以使用"+(5-count)+"次本软件。");
		count++;
		pro.setProperty("count", count+"");
		FileOutputStream writer=new FileOutputStream(confile);
		pro.store(writer, "comments");
		writer.close();
		in.close();
	}
}



这篇关于JAVA之IO技术相关 如何设置软件的使用次数的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程