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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 奋发吧小白 高级黑马   /  2014-11-5 14:15  /  646 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. package IO流练习;
  2. import java.io.*;
  3. import java.util.*;
  4. public class 计数器 {
  5.         public static void main(String[] args) throws Exception{
  6.                 useCount();
  7.         }
  8.         public static void useCount() throws Exception
  9.         {
  10.                 //把目标文件封装成对象
  11.                 File file = new File("D:\\pz.in");
  12.                 if(!file.exists())
  13.                 {
  14.                         file.createNewFile();
  15.                 }
  16.                 //定义一个计数器
  17.                 int count = 0;
  18.                 Properties prop = new Properties();
  19.                 BufferedInputStream bis = new BufferedInputStream(
  20.                                 new FileInputStream(file));
  21.                 prop.load(bis);
  22.                 String value = prop.getProperty("time");
  23.                 if(value!=null)
  24.                 {
  25.                         count  = Integer.parseInt(value);
  26.                         if(count>=3)
  27.                         {
  28.                                 System.out.println("你的使用次数已到,请注册!");
  29.                                 return ;
  30.                         }
  31.                 }
  32.                 count++;
  33.                 prop.setProperty("time",count+"");
  34.                 BufferedOutputStream bos = new BufferedOutputStream(
  35.                                 new FileOutputStream(file));
  36.                 prop.store(bos, "comments");
  37.                 bis.close();
  38.                 bos.close();
  39.                
  40.         }
  41. }
复制代码




评分

参与人数 1黑马币 +1 收起 理由
杨佳名 + 1

查看全部评分

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马