Java:Properties集合

文章目录

介绍

Properties属性集合
特点:
    存储属性名和属性值
    属性名和属性值都是字符串类型
    没有泛型
    和流有关

Properties用法

/**
 * 创建Properties集合:Properties properties = new Properties();
 * 添加数据:properties.setProperty("属性名","属性值");
 * 获取数据:System.out.println(properties.getProperty("username"));
 * 遍历:1.keySet遍历
 *          for (Object each:properties.keySet()){
 *             System.out.println(each + "---" + properties.get(each));
 *         }
 *      2.entrySet遍历
 *          for (Map.Entry<Object, Object> each:properties.entrySet()){
 *             System.out.println(each.getKey() + "---" + each.getValue());
 *         }
 *      3.stringPropertyNames遍历
 *          for (String each: properties.stringPropertyNames()){
 *             System.out.println(each + "---" + properties.getProperty(each));
 *         }
 *
 * 和流有关的方法:
 *     1.list方法 输出到文件
 *         PrintWriter pw = new PrintWriter("E:\\JAVA\\day\\src\\Properties");
 *         properties.list(pw);
 *         pw.close();
 *     2.store方法(不能存在中文) 输出到文件
 *         FileOutputStream fos = new FileOutputStream("E:\\JAVA\\day\\src\\Properties\\store.properties");
 *         properties.store(fos,""); //注释不能是中文
 *         fos.close();
 *     3.load方法 输入到Properties集合中
 *         Properties properties1 = new Properties();
 *         FileInputStream fis = new FileInputStream("E:\\JAVA\\day\\src\\Properties\\store.properties");
 *         properties1.load(fis);
 *         fis.close();
 *         System.out.println(properties1.toString());
 *
 */
public static void main(String[] args) throws Exception{
    //1.创建集合
    Properties properties = new Properties();
    //2.添加数据 获取数据
    properties.setProperty("username","张三");
    properties.setProperty("age","21");
    System.out.println(properties.toString());
    System.out.println(properties.getProperty("username"));
    //3.遍历
    //3.1---keySet---
    for (Object each:properties.keySet()){
        System.out.println(each + "---" + properties.get(each));
    }
    //3.2---entrySet---
    for (Map.Entry<Object, Object> each:properties.entrySet()){
        System.out.println(each.getKey() + "---" + each.getValue());
    }
    //3.3---stringPropertyNames---
    for (String each: properties.stringPropertyNames()){
        System.out.println(each + "---" + properties.getProperty(each));
    }

    //4.和流有关的方法
    //----------list方法---------
    PrintWriter pw = new PrintWriter("E:\\JAVA\\day\\src\\Properties\\Print.txt");
    properties.list(pw);
    pw.close();
    //----------store方法--------
    FileOutputStream fos = new FileOutputStream("E:\\JAVA\\day\\src\\Properties\\store.properties");
    properties.store(fos,"注释");
    fos.close();
    //----------load方法--------
    Properties properties1 = new Properties();
    FileInputStream fis = new FileInputStream("E:\\JAVA\\day\\src\\Properties\\store.properties");
    properties1.load(fis);
    fis.close();
    System.out.println(properties1.toString());
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值