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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 寇弘禄 中级黑马   /  2013-4-6 10:39  /  1166 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 张熙韬 于 2013-4-6 21:32 编辑

这段代码为什么计数器不自增呢?
  1. public class RunCountDemo {

  2.         /**
  3.          * 定义一个程序用于记录软件的使用次数。
  4.          * 思路是在硬盘上定义一个配置文件,通过键值对的形式存储。
  5.          * 1 定义一个Properties集合
  6.          * 2 定义一个字节流将配置文件的数据加载进集合。
  7.          * 3 通过键tiem获取已使用次数。
  8.          * 4 定义一个计数器。
  9.          * 5如果获取到的值不为空则将此值付给计数器,先判断此值是否小于5,如果不小于5,则退出,否则计数器自增后将键和值存入。
  10.          * 6否则计数器自增后将键和值存入。
  11.          * 7定义输出流将配置文件输出。
  12.          * @throws IOException
  13.          */
  14.         public static void main(String[] args) throws IOException {
  15.                 RunCountMethod();

  16.         }
  17.         public static void RunCountMethod() throws IOException{
  18.                 Properties prop = new Properties();
  19.                 File file = new File("d:\\count.properties");
  20.                 if(!file.exists()){
  21.                         throw new RuntimeException("文件不存在");
  22.                 }
  23.                 FileInputStream fis = new FileInputStream(file);
  24.                 FileOutputStream fos = new FileOutputStream(file);
  25.                
  26.                 int count = 0;
  27.                 prop.load(fis);
  28.                 String value = prop.getProperty("time");
  29.                 if(value!=null){
  30.                         System.out.println("value!=null");
  31.                         count = Integer.parseInt(value);
  32.                         if(count>=4){
  33.                                 System.out.println("使用次数已到,请付费");
  34.                                 return;
  35.                         }
  36.                 }
  37.                 System.out.println("count=="+count);  //为什么count总是0
  38.                 count++;
  39.                 prop.setProperty("time", count+"");
  40.                 prop.store(fos, "");
  41.                 fos.close();
  42.                 fis.close();
  43.         }
  44. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
田磊阳 + 1

查看全部评分

2 个回复

倒序浏览
本帖最后由 吴林飞 于 2013-4-6 11:39 编辑
  1. import java.io.*;
  2. import java.util.*;
  3. public class RunCountDemo {

  4.         /**
  5.          * 定义一个程序用于记录软件的使用次数。
  6.          * 思路是在硬盘上定义一个配置文件,通过键值对的形式存储。
  7.          * 1 定义一个Properties集合
  8.          * 2 定义一个字节流将配置文件的数据加载进集合。
  9.          * 3 通过键tiem获取已使用次数。
  10.          * 4 定义一个计数器。
  11.          * 5如果获取到的值不为空则将此值付给计数器,先判断此值是否小于5,如果不小于5,则退出,否则计数器自增后将键和值存入。
  12.          * 6否则计数器自增后将键和值存入。
  13.          * 7定义输出流将配置文件输出。
  14.          * @throws IOException
  15.          */
  16.         public static void main(String[] args) throws IOException {
  17.                 RunCountMethod();

  18.         }
  19.         public static void RunCountMethod() throws IOException{
  20.                 Properties prop = new Properties();
  21.                 File file = new File("d:\\count.properties");
  22.                 if(!file.exists()){
  23.                         throw new RuntimeException("文件不存在");
  24.                 }
  25.                 FileInputStream fis = new FileInputStream(file);
  26.                 //FileOutputStream fos = new FileOutputStream(file);
  27.                 //你这里直接创建两个流和file关联有意义么,输入流中的数据还没有经过计数器自增操作,就直接转到输出流中
  28.                 //计数器一直就没起作用。
  29.                 int count = 0;
  30.                 prop.load(fis);
  31.                 String value = prop.getProperty("time");
  32.                 if(value!=null){
  33.                         System.out.println("value!=null");
  34.                         count = Integer.parseInt(value);
  35.                         if(count>=4){
  36.                                 System.out.println("使用次数已到,请付费");
  37.                                 return;
  38.                         }
  39.                 }
  40.                 System.out.println("count=="+count);  //为什么count总是0
  41.                 count++;
  42.                                 FileOutputStream fos = new FileOutputStream(file);//
  43.                 prop.setProperty("time", count+"");
  44.                 prop.store(fos, "");
  45.                 fos.close();
  46.                 fis.close();
  47.         }
  48. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
田磊阳 + 1

查看全部评分

回复 使用道具 举报
吴林飞 发表于 2013-4-6 11:35

在上面定义了,到下面在用不行吗?不是读到  prop.store(fos, ""); 才用到这个fos流吗?
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马