黑马程序员技术交流社区

标题: Properties中几处不解之处 [打印本页]

作者: zhouchao    时间: 2013-7-7 16:48
标题: Properties中几处不解之处
本帖最后由 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+""   


作者: 孙金鑫    时间: 2013-7-7 17:18
问题1:求解 count = Integer.parseInt(value);这句代码的意义
是把字符串转换成Integer类型的数据。

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

所以你问的两个问题,两者相辅相成。
作者: "O_忆_O    时间: 2013-7-7 17:31
count = Integer.parseInt(value);的意义:
由于在文件中,time的值是以字符串形式存储的,所以取出来之后要进行数据类型转化,使它变为int类型再存入count中。
count+""这句也是数据类型转化,将int类型数据转化为String类型然后存回文件中。
作者: zhouchao    时间: 2013-7-7 17:34
孙金鑫 发表于 2013-7-7 17:18
问题1:求解 count = Integer.parseInt(value);这句代码的意义
是把字符串转换成Integer类型的数据。

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

非常感谢!
作者: 孙金鑫    时间: 2013-7-7 17:40
zhouchao 发表于 2013-7-7 17:34
非常感谢你的回答!!  是不是因为 value的类型不是int型的 所以要转换成int型的 ...

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

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

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

谢谢指点!!!




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2