Properties是Hashtable的子类

来源:互联网 发布:淘宝怎么上传视频凭证 编辑:程序博客网 时间:2024/05/21 10:17
/**
     * @param args
     * Properties是Hashtable的子类
     * @throws IOException
     * @throws FileNotFoundException
     */
    public static void main(String[] args) throws FileNotFoundException, IOException {
        //demo1();
        //demo2();
        Properties prop = new Properties();
        prop.load(new FileInputStream("config.properties"));        //将文件上的键值对读取到集合中
        prop.setProperty("tel", "18912345678");
        prop.store(new FileOutputStream("config.properties"), null);//第二个参数是对列表参数的描述,可以给值,也可以给null
        System.out.println(prop);
    }

    public static void demo2() {
        Properties prop = new Properties();
        prop.setProperty("name", "张三");
        prop.setProperty("tel", "18912345678");
        
        //System.out.println(prop);
        Enumeration<String> en = (Enumeration<String>) prop.propertyNames();
        while(en.hasMoreElements()) {
            String key = en.nextElement();                //获取Properties中的每一个键
            String value = prop.getProperty(key);        //根据键获取值
            System.out.println(key + "="+ value);
        }
    }

    public static void demo1() {
        Properties prop = new Properties();
        prop.put("abc", 123);
        System.out.println(prop);
    }

原创粉丝点击