A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始



  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.util.Properties;

  7. /*
  8. * 记录应用程序运行的次数,如果次数已经达到要求,那么就给出注册提示
  9. */

  10. public class RunCountDemo {

  11.         public static void main(String[] args) {

  12.                 // 创建一个无默认值的空属性列表
  13.                 Properties pro = new Properties();

  14.                 // 新建配置文件
  15.                 File file = new File("RunCount.ini");
  16.                 if (!file.exists())
  17.                         try {
  18.                                 file.createNewFile();
  19.                         } catch (IOException e) {
  20.                                 e.printStackTrace();
  21.                         }

  22.                 // 创建输入流加载配置文件中的信息
  23.                 FileInputStream fis = null;
  24.                 try {
  25.                         fis = new FileInputStream(file);
  26.                 } catch (FileNotFoundException e) {
  27.                         e.printStackTrace();
  28.                 }

  29.                 // 将配置文件中的信息加载到Properties中
  30.                 try {
  31.                         pro.load(fis);
  32.                 } catch (IOException e) {
  33.                         e.printStackTrace();
  34.                 }finally{
  35.                         if(fis !=null)
  36.                                 try {
  37.                                         fis.close();
  38.                                 } catch (IOException e) {
  39.                                         e.printStackTrace();
  40.                                 }
  41.                 }

  42.                 int count = 0;
  43.                 String value = pro.getProperty("RunTime");
  44.                 if (value != null) {
  45.                         count = Integer.parseInt(value);
  46.                         // 判定使用次数
  47.                         if (count >= 3) {
  48.                                 System.out.println("已经使用3次,请填写注册信息!");
  49.                                 return;
  50.                         }
  51.                 }
  52.                 count++;
  53.                 pro.setProperty("RunTime", Integer.toString(count));

  54.                 // 创建输出流输出配置文件中的信息
  55.                 FileOutputStream fos = null;
  56.                 try {
  57.                         fos = new FileOutputStream(file);
  58.                 } catch (FileNotFoundException e) {
  59.                         e.printStackTrace();
  60.                 }
  61.                 try {
  62.                         pro.store(fos,"软件使用次数");
  63.                 } catch (IOException e) {
  64.                         e.printStackTrace();
  65.                 }finally{
  66.                         if(fos !=null)
  67.                                 try {
  68.                                         fos.close();
  69.                                 } catch (IOException e) {
  70.                                         e.printStackTrace();
  71.                                 }
  72.                 }
  73.         }
  74. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
lwj123 + 1

查看全部评分

2 个回复

正序浏览
不错啊!
回复 使用道具 举报
考察 Properties 的用法
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马