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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© mm446899 中级黑马   /  2014-12-15 23:55  /  792 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

需求:记录应用程序的使用次数,如果使用次数已到,则提示用户注册。可以不用 io流吗
  1. public static void main(String[] args) throws IOException{
  2.                 Properties prop = new Properties();//定义Properties,用来和IO流结合
  3.                 File file = new File("library\\time.ini");//配置文件
  4.                 if(!file.exists())
  5.                         file.createNewFile();//如果文件不存在则创建文件(用于第一次使用时创建文件)
  6.                 FileInputStream fis = new FileInputStream(file);//定义字节读取流,读取配置文件中记录的使用次数
  7.                 prop.load(fis);//载入流,以获取文件中配置的键值对
  8.                 int count = 0;//定义使用次数
  9.                 String countValue = prop.getProperty("time");//通过键获取值
  10.                 if(countValue!=null){//第一次时countValue为null
  11.                         count = Integer.parseInt(countValue);//将字符串次数变成数字次数
  12.                         if(count>3){
  13.                                 System.out.println("您使用次数已到,继续使用请注册!");
  14.                                 return;
  15.                         }
  16.                 }
  17.                 count++;//如果使用次数未到则次数加1
  18.                 prop.setProperty("time", count+"");//配置新的键值对
  19.                 FileWriter fos = new FileWriter(file);
  20.                 prop.store(fos, "这是应用程序使用次数的配置文件");//将新的键值对写入文件
  21.                 fis.close();
  22.                 fos.close();       
  23.         }
复制代码

1 个回复

倒序浏览
如果不用IIO流的话,数据保存在哪里呢?内存中?内存中的数据关机之后会清空啊
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马