Properties类

表示一个持久的属性集

持久的:可以保存到流中,也可以从流中读取。可以很方便和文件进行交互(存储到文 件中)
是一个Map体系的集合类,是Hashtable的子类

public class PropertiesExample {
    public static void main(String[] args) {
        //Properties - > property - > prop
        Properties properties = new Properties();
        properties.put("1","哈哈");
        properties.put("2",1);
        properties.put("3",new Student(1,"123",12)); //这种类型存储会出现问题
        System.out.println(properties);
    }
}

方法
Object setProperty(String key, String value) 设置集合的键和值,都是Stri ng类型,底层调用Hashtable方法 put
String getProperty(String key) 使用此属性列表中指定的键搜索属性
Set stringPropertyNames() 从该属性列表中返回一个不可修改的键集,其中键及 其对应的值是字符串

Properties properties = new Properties();

properties.setProperty("name","李四");
System.out.println(properties.getProperty("name"));

Set<String> set = properties.stringPropertyNames();
set.forEach(System.out::println);

在这里插入图片描述

public class PropertiesExample3 {
	public static void main(String[] args) throws Exception {
		Properties properties = new Properties();
        
		properties.setProperty("name","李四");
		properties.setProperty("age","88");
        
        properties.setProperty("hobby","抽烟喝酒烫头");
		//存储数据 - 写文件 - 输出流
		properties.store(new FileOutputStream("src/com/its/db.properties"),"说明xxx");
		// properties.store(new FileWriter("src/com/its/db.properties"),"说明xxx");//这段代码对输入汉字友好
		Properties news = new Properties();
		//读取数据 - 读文件 - 输入流
		news.load(new FileInputStream("src/com/its/db.properties"));
		//news.load(new FileReader("src/com/its/db.properties"));//这段代码对输出汉字友好
		System.out.println(news);
	}
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值