- /*
- * 需求:运行程序运行五次之后,不可在运行,抛出 “使用次数已到,请购买正版”
- * 思路:将运行五次的信息存储到程序本身 useCount——5 这种形式,对应关系可以用类Properties来存储
- * setProperty(k,v),读取文件信息,load(InputStream);存储配置信息,store(OutputStream),每次运行count+1
- */
- public class PropTest
- {
- public static void main(String[] args) throws IOException
- {
- File fileName = new File("d:\\propTest.properties");
- openFile(fileName);
- }
- public static void openFile(File fileName) throws IOException
- {
- FileOutputStream fos = new FileOutputStream(fileName);
- Properties prop = new Properties();
- FileInputStream fis = new FileInputStream(fileName);
- prop.load(fis);
- String key = "useCount";
- System.out.println("判断了吗");
- if(prop.containsKey(key))
- {
- System.out.println("if----run");
- String value = prop.getProperty(key);
- int count = Integer.parseInt(value);
- if(count>4)
- {
- throw new RuntimeException("已到使用次数上限");
- }
- count++;
- prop.setProperty(key, count+"");
- prop.store(fos, "");
- }else
- {
- System.out.println("else----run");
- prop.setProperty(key, 1+"");
- prop.store(fos, "");
- }
- fos.close();
- fis.close();
- }
- }
复制代码 |