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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 小路飞 中级黑马   /  2013-3-22 20:52  /  1052 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 侯国奇 于 2013-3-23 08:41 编辑
  1. import java.io.*;
  2. import java.util.*;
  3. class RunCount
  4. {
  5. public static void main(String[] args) throws IOException
  6. {
  7. Properties prop = new Properties();

  8. File file = new File("count.ini");
  9. if(!file.exists())
  10. file.createNewFile();

  11. FileInputStream fis = new FileInputStream(file);

  12. prop.load(fis);


  13. int count = 0;
  14. String value = prop.getProperty("time");//"time"这个键从何而来?

  15. if(value!=null)
  16. {
  17. count = Integer.parseInt(value);
  18. if(count>=5)
  19. {
  20. System.out.println("您好,使用次数已到,拿钱!");
  21. return ;
  22. }

  23. }

  24. count++;

  25. prop.setProperty("time",count+"");

  26. FileOutputStream fos = new FileOutputStream(file);

  27. prop.store(fos,"");

  28. fos.close();
  29. fis.close();

  30. }
  31. }
复制代码
就是想问问程序中"time"这个键从何而来?
谢谢了~

评分

参与人数 1技术分 +1 收起 理由
黄玉昆 + 1

查看全部评分

3 个回复

倒序浏览
这个是毕向东的那个什么用5次就不上用了 那个例子是吧 这个是他手动写进去的  是count.ini合格文件他用记事本打开 手动敲进去的  我记着是  你再看看他创建这个文件的时候
回复 使用道具 举报
09.File file = new File("count.ini");//关联配置文件内容如: time=xx

10.if(!file.exists())

11.file.createNewFile();

12.

13.FileInputStream fis = new FileInputStream(file);

14.

15.prop.load(fis);//把配置文件数据加载到该集合:Properties继承了Map集合,是一个容器,所以他里面的的数据都是以键值对的形式存在的

16.

17.

18.int count = 0;

19.String value = prop.getProperty("time");//"time"这个键从何而来?//该time是从count.ini读取到的,在此是以time为键,获取其对应的值
另外Properties的其他属性可以查阅API
回复 使用道具 举报
楼主请看下面代码红蓝色字体:

import java.io.*;
import java.util.*;

class RunCount {
        public static void main(String[] args) throws IOException {
                Properties prop = new Properties();

                File file = new File("count.ini");
                if (!file.exists())
                        file.createNewFile();

                FileInputStream fis = new FileInputStream(file);
                prop.load(fis);
                int count = 0;
                String value = prop.getProperty("time");// "time"这个键从何而来?

                if (value != null)                                      //如果能找到time键,则value不为null,执行if代码块     
                     {                                                            
                        count = Integer.parseInt(value);

                        if (count >= 5) {
                                System.out.println("您好,使用次数已到,拿钱!");
                                return;      }
                    }

                count++;                                               //如果不能找到time键,则添加time,设置其值为count
                prop.setProperty("time", count + "");
                FileOutputStream fos = new FileOutputStream(file);

                prop.store(fos, "");                                 //保存properties

                fos.close();
                fis.close();
        }
}

评分

参与人数 1技术分 +1 收起 理由
黄玉昆 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马