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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 晓天s 中级黑马   /  2013-10-8 21:23  /  1503 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

Properties
(1)Properties是HashTable的子类,具备Map集合的特点,里面存储的是键值对
(2)Properties是IO流合集合相结合的集合容器
(3)Properties的特点是可以用于存储键值对形式的配置文件
(4)构造方法:
        Properties()
                创建一个无默认值的空属性列表。
        Properties(Properties defaults)
                创建一个带有指定默认值的空属性列表。
(5)方法摘要:
        Object setProperty(String key, String value)
                调用 Hashtable 的方法 put。
        String getProperty(String key)
                用指定的键在此属性列表中搜索属性。
        void load(InputStream inStream)
                从输入流中读取属性列表(键和元素对)。
        void load(Reader reader)
                按简单的面向行的格式从输入字符流中读取属性列表(键和元素对)。
        void list(PrintStream out)
                将属性列表输出到指定的输出流。
        void list(PrintWriter out)
                将属性列表输出到指定的输出流。
        void store(OutputStream out, String comments)
                以适合使用 load(InputStream) 方法加载到 Properties 表中的格式,
                将此 Properties 表中的属性列表(键和元素对)写入输出流。
        void store(Writer writer, String comments)
                以适合使用 load(Reader) 方法的格式,将此 Properties 表中的
                属性列表(键和元素对)写入输出字符。
        Set<String> stringPropertyNames()
                返回此属性列表中的键集,其中该键及其对应值是字符串,如果在主属性列表中
                未找到同名的键,则还包括默认属性列表中不同的键
(6)Properties代码示例:
        public static void show()
        {
                Properties prop = new Properties();
                prop.setProperty("张三","26");
                prop.setProperty("李四","30");
                prop.setProperty("王五","35");
                sop(prop);
                String value = prop.getProperty("张三");

                Set<String> keys = prop.stringPropertyName();
                for(String key : values)
                {
                        sop(key+":"+prop.getPropety(key));
                }
        }
(7)需求:记录应用程序的使用次数,如果使用次数已到,则提示用户注册。
   思路:
   **第一次使用时建立一个配置文件用于记录使用次数
   **每次使用都加载该配置文件,并先判断已使用次数
   **每次使用完使用次数加1,写入配置文件
        public static void main(String[] args) throws IOException{
                Properties prop = new Properties();//定义Properties,用来和IO流结合
                File file = new File("library\\time.ini");//配置文件
                if(!file.exists())
                        file.createNewFile();//如果文件不存在则创建文件(用于第一次使用时创建文件)
                FileInputStream fis = new FileInputStream(file);//定义字节读取流,读取配置文件中记录的使用次数
                prop.load(fis);//载入流,以获取文件中配置的键值对
                int count = 0;//定义使用次数
                String countValue = prop.getProperty("time");//通过键获取值
                if(countValue!=null){//第一次时countValue为null
                        count = Integer.parseInt(countValue);//将字符串次数变成数字次数
                        if(count>3){
                                System.out.println("您使用次数已到,继续使用请注册!");
                                return;
                        }
                }
                count++;//如果使用次数未到则次数加1
                prop.setProperty("time", count+"");//配置新的键值对
                FileWriter fos = new FileWriter(file);
                prop.store(fos, "这是应用程序使用次数的配置文件");//将新的键值对写入文件
                fis.close();
                fos.close();       
        }

1 个回复

正序浏览
To 金牌黑马 2013-10-9 08:56:35
沙发
支持楼主。继续加油
回复 使用道具 举报 1 0
您需要登录后才可以回帖 登录 | 加入黑马