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

来源:互联网 发布:js的find方法 编辑:程序博客网 时间:2024/06/05 23:47

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

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();}}


 

原创粉丝点击