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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© zhouchao 中级黑马   /  2013-7-7 16:48  /  1613 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 zhouchao 于 2013-7-7 17:35 编辑

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.creatNewFile();

        FileInputStream fis = new FileInputStream(file);
        prop.load(fis);

        int count = 0;
        String value = prop.getProperty("time");

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

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

        FileOutputStream fos = new FileOutputStream(file)

        prop.store(fos,"")

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


求解 count = Integer.parseInt(value);这句代码的意义
还有prop.setProperty("time",count+"")这个最后为什么是count+""   

评分

参与人数 1技术分 +1 收起 理由
杨兴庭 + 1

查看全部评分

6 个回复

倒序浏览
问题1:求解 count = Integer.parseInt(value);这句代码的意义
是把字符串转换成Integer类型的数据。

问题2:还有prop.setProperty("time",count+"")这个最后为什么是count+""
setProperty(),设置Properties配置文件里面的属性,count+"",是把Integer类型的数据转换成字符串。

所以你问的两个问题,两者相辅相成。

评分

参与人数 1技术分 +1 收起 理由
杨兴庭 + 1

查看全部评分

回复 使用道具 举报
count = Integer.parseInt(value);的意义:
由于在文件中,time的值是以字符串形式存储的,所以取出来之后要进行数据类型转化,使它变为int类型再存入count中。
count+""这句也是数据类型转化,将int类型数据转化为String类型然后存回文件中。
回复 使用道具 举报
孙金鑫 发表于 2013-7-7 17:18
问题1:求解 count = Integer.parseInt(value);这句代码的意义
是把字符串转换成Integer类型的数据。

非常感谢你的回答!!  是不是因为 value的类型不是int型的 所以要转换成int型的
回复 使用道具 举报
"O_忆_O 发表于 2013-7-7 17:31
count = Integer.parseInt(value);的意义:
由于在文件中,time的值是以字符串形式存储的,所以取出来之 ...

非常感谢!
回复 使用道具 举报
zhouchao 发表于 2013-7-7 17:34
非常感谢你的回答!!  是不是因为 value的类型不是int型的 所以要转换成int型的 ...

String getProperty(String key)
          用指定的键在此属性列表中搜索属性。

Object setProperty(String key, String value)
          调用 Hashtable 的方法 put。  

你说的对,因为count每次运行调用都会运算,所以在配置文件里面读取,需要转换类型。
回复 使用道具 举报
孙金鑫 发表于 2013-7-7 17:40
String getProperty(String key)
          用指定的键在此属性列表中搜索属性。

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