程序试用次数的总流程

1.创建方法,实现程序运行

2主方法中判断是否到达次数来结束程序的试用

3判断条件单独创建方法

4.在判断条件方法中:

  4.1 创建出一个存放程序信息的配置文件,当然此时它为空

  4.2 创建一个属性对象

  4.3 创建一个String类型的Value引用对象,把属性对象中的用键得值得方法赋予给它

  4.4加入一个计数器 int  count =0; 此计数器在程序第一次运行的时候有用,此时 配置文件中来没有该计数器的信息

  4.5判断Value是否为空,是的话,直接跳到下一行代码 也就是计数器自增;如果不为空了 我们就把配置文件中的计数器的值拿出来赋值给内存中的count,并且

  去判断计数器是否满足次数要求了,如果满足了就return true 来结束程序

  4.6 计数器自增

  4.7调用Properties对象中的设置键值方法

  4.8 设置一个路径为配置文件的输出流

  4.9调用properties对象中的储存方法,把计数器信息存储起来

计数器是难点啊。。。。。。。。PS:想了半天的样子

package com.java.Procedure.www;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ProcedureDmeo {

 public static void main(String[] args) throws IOException {
  //判断程序运行次数是否到达试用次数
  if(isStop()){
   System.out.println("使用次数已到,请注册");
   return;
  }
  // 启动程序
  procedureRun();
  
 }
 /**
  * 存储运行次数并且判断是否到达次数上限
  * @return
  * @throws IOException
  */
 private static boolean isStop() throws IOException {
  //创建配置文件
  File configFile=new File("D:\ja\app.properties");
  //判断存在不
  if(!configFile.exists()){
   configFile.createNewFile();
  }
  //创建一个输出流
  FileInputStream fis=new FileInputStream(configFile);
  //创建属性
  Properties prop=new Properties();
  //加载
  prop.load(fis);
  String value=prop.getProperty("count");
  int count = 0;//作为标记而已 第一次使用 程序第二次运行时因为调用了属性集中的就不再使用此
  if(value!=null){
   count=Integer.parseInt(value);
   if(count>6){
    return true;
   } 
  }
  count++;
  prop.setProperty("count", Integer.toString(count));//设置属性
  FileOutputStream fos=new FileOutputStream(configFile);//设置个输出流
  //固有化
  prop.store(fos, "properties");
  //关流
  fis.close();
  fos.close();
  return false;
 }
 /**
  * 程序运行
  * @param file
  * @throws IOException
  */
 private static void procedureRun() {
  
  
  System.out.println("程序启动运行"); 
 }
}

  

原文地址:https://www.cnblogs.com/daoxiang1992/p/5781825.html