首页 > 其他 > 详细

记录程序启动次数,达到固定次数后不能使用

时间:2014-03-15 05:59:10      阅读:476      评论:0      收藏:0      [点我收藏+]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<br><br>import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
public class PropertiesTest {
    public static void main(String[] args) throws IOException {
        File file = new File("my.ini");
        //犯傻的时候,我把这句写到这,还说每次读到的文件都是空的,傻了
        //这样的话,每次程序一运行,就会覆盖以前有内容的配置文件
        //FileWriter fileW = new FileWriter(file);
        if(!file.exists()){
            file.createNewFile();
        }
        System.out.println(file.exists());
        Properties ps = new Properties();
        ps.load(new FileReader(file));
        int count = 0;
        ps.list(System.out);
        String countStr = ps.getProperty("count");
        if(countStr != null){
            count = Integer.valueOf(ps.getProperty("count"));
            if(count > 3){
                throw new RuntimeException("您已经登录3次,请付钱!");
            }
        }
        count ++;
        ps.setProperty("count", String.valueOf(count));
        ps.store(new FileWriter(file), "properties");
 
    }
 
}

  思路:

  给启动程序添加配置文件,用于记录程序启动次数,每次启动,修改启动次数,当达到指定次数时抛出异常

记录程序启动次数,达到固定次数后不能使用,布布扣,bubuko.com

记录程序启动次数,达到固定次数后不能使用

原文:http://www.cnblogs.com/lxricecream/p/3601217.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!