zoukankan      html  css  js  c++  java
  • Properties 类的使用

    Properties 类的使用

    定义: 

      表示一个持久的集合,可以存在流中,或者从流中加载。是Hashtable子类,map集合方法都可以用。

    方法的使用:

    复制代码
    /*
     * 集合对象 properties继承Hashtable实现了Map接口
     * 实现数据的永久存储 store
     * */
    public class PropertiesDemo {
        public static void main(String[] args) throws Exception {
            fun3();
        }
        //1.关于Properties集合  添加数据   获得数据    遍历数据
        public static void fun1(){
            Properties p=new Properties();
            p.setProperty("a", "1");
            p.setProperty("a1", "11");
            p.setProperty("a2", "111");
            //将集合中的键,存储到Set集合
            Set<String> set=p.stringPropertyNames();
            for(String k:set){
                System.out.println(k+"   "+p.getProperty(k));
            }
        }
        //2.Properties集合特有的方法,load(InputStream in)   load(Reader r)
        //流对象读取键值对
        public static void fun2() throws Exception{
            Properties p=new Properties();
            FileReader f=new FileReader("c:\1.properties");
            p.load(f);//读取c:\1.properties
            f.close();
            System.out.println(p);
        }
        //3.存 store(OutputStream out)   store(Writer w)
        public static void fun3() throws IOException{
            Properties p=new Properties();
            p.setProperty("name", "张三");
            p.setProperty("age", "22");
            p.setProperty("name1", "张三");
            //键不能相同,值可以相同,否则会覆盖
            p.setProperty("name", "张三");
            p.setProperty("name", "张三");
            FileWriter f=new FileWriter("c:\1.properties");
            p.store(f, "");//存到c:\1.properties
            f.close();
        }
    }
    复制代码
  • 相关阅读:
    版本号中Snapshot的含义
    Spring Security 过滤器链
    Spring Security入门
    签个到
    Centos下Mysql密码忘记解决办法
    idea 修改静态资源不需要重启的办法
    X509证书信任管理器类的详解
    一、DES加密和解密
    Spring Boot中的事务管理
    SpringBoot JPA实现增删改查、分页、排序、事务操作等功能
  • 原文地址:https://www.cnblogs.com/cuichaobo/p/10681329.html
Copyright © 2011-2022 走看看