Java之Properties集合

Properties介绍

  • 是一个Map体系的集合类(该类继承Hashtable类 ,Hashtable该类实现了Map接口)

  • Properties可以保存到流中或从流中加载

  • 属性列表中的每个键及其对应的值都是一个字符串

1.基本使用:

public class PropertiesDemo01 {
    public static void main(String[] args) {
        //创建集合对象
//        Properties<String,String> prop = new Properties<String,String>(); //错误
        Properties prop = new Properties();

        //存储元素
        prop.put("itheima001", "佟丽娅");
        prop.put("itheima002", "赵丽颖");
        prop.put("itheima003", "刘诗诗");

        //遍历集合
        Set<Object> keySet = prop.keySet();
        for (Object key : keySet) {
            Object value = prop.get(key);
            System.out.println(key + "," + value);
        }
    }
}

该类拥有Map方法,添加就map方法,put。具体MapJavaMap集合_華同学.的博客-CSDN博客

 2.Properties作为Map集合的特有方法

特有方法

方法名说明
Object setProperty(String key, String value)设置集合的键和值,都是String类型,底层调用 Hashtable方法 put
String getProperty(String key)使用此属性列表中指定的键搜索属性
Set<String> stringPropertyNames()从该属性列表中返回一个不可修改的键集,其中键及其对应的值是字符串
public class PropertiesDemo02 {
    public static void main(String[] args) {
        //创建集合对象
        Properties prop = new Properties();

        //Object setProperty(String key, String value):设置集合的键和值,都是String类型
        prop.setProperty("it001", "佟丽娅");
        prop.setProperty("it002", "赵丽颖");
        prop.setProperty("it003", "刘诗诗");

        //String getProperty(String key):使用此属性列表中指定的键搜索属性
//        System.out.println(prop.getProperty("it001"));
//        System.out.println(prop.getProperty("it0011"));

//        System.out.println(prop);

        //Set<String> stringPropertyNames():从该属性列表中返回一个不可修改的键集,其中键及其对应的值是字符串
        Set<String> names = prop.stringPropertyNames();
        for (String key : names) {
//            System.out.println(key);
            String value = prop.getProperty(key);
            System.out.println(key + "," + value);
        }
    }
}

 3.properties和IO流相结合的方法*****

方法名说明
void load(Reader reader)从输入字符流读取属性列表(键和元素对)
void store(Writer writer, String comments)将此属性列表(键和元素对)写入此 Properties表中,以适合使用 load(Reader)方法的格式写入输出字符流

 

要创建properties 类型的文件 进行操作 读取或者写入(写入的情况很少)

 配置文件:

        xml   <开始标签></结束标签>

        properties 

                key=value

                key1=value1

        yaml

                key:value

                key1:value1

注:这些配置文件 支持项目运行的一些数据,

读数据:

public class Demo01 {
    public static void main(String[] args) throws IOException {
        Properties pp = new Properties();
//        加载文件到集合中
//        pp.load(new FileInputStream("day12\\src\\ccc.properties"));
        FileReader fr = new FileReader("day12\\src\\ccc.properties");
        pp.load(fr);
//        pp.load();
        fr.close();


//        获取所有key
        Set<String> set = pp.stringPropertyNames();
//        遍历
        for (String s : set) {
            System.out.println(s + "->" + pp.getProperty(s));
        }

    }
}

 写数据:

public class Demo02 {
    public static void main(String[] args) throws IOException {
        Properties pp = new Properties();
        pp.setProperty("name","张三");
        pp.setProperty("age","19");
        pp.setProperty("sex","男");
//        FileOutputStream fos = new FileOutputStream("day12\\src\\eee.properties");
        FileWriter fw = new FileWriter("day12\\src\\eee.properties");
//        pp.store(fos,"key=value");
//        pp.store(fos,null);
        pp.store(fw,null);
        fw.close();
    }
}

  pp.store(fw,null);  /  pp.store(fos,"key=value");  这里的null   或后面的字符串 是文件上边的注释信息,null就为不写  “key=value”

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

華同学.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值