java分享第七天-01(Hashmap和Hashtable的区别&Property)

本文详细对比了Hashmap与Hashtable的区别,包括线程安全性、父类、null值处理等方面,并介绍了Property类的应用场景及其核心方法,通过示例展示了如何用Properties类读写配置文件。

一、Hashmap和Hashtable的区别

1 主要:Hashtable线程安全,同步,效率相对低下

           HashMap线程不安全,非同步,效率相对高

2 父类:Hashtable是Dictionary   HashMap是AbstractMap

3 rull:Hashtable键与值不能为null

HashMap键最多一个null,值可心多个null

二  Property

1 作用:读写资源配置文件

2 键与值只能为字符串

3 方法:

setProperty(String key,String value)

getProperty(String key)

getProperty(String key,String defaultValue)

后缀:properties

store(OutputStream out,String coomments)

store(Writer writer,String coomments)

load(inputStream instream)

load(Reader  reader)

.xml

storeToXml(OutputStream os,String coomments):UTF-8字符集

storeToXml(OutputStream os,String coomments,String encoding)

loadFromXML(inputStream in)

     /*运用Properties输出到文件*/
    public static void main(String[] args) throws FileNotFoundException, IOException {
    //创建对象
    Properties properties=new Properties();
    //存储
    properties.setProperty("driver", "oracle.jdbc.driver.OracleDriver");
    //存储到e:/others绝对路径 
    properties.store(new FileOutputStream(new File("e:/others/db.properties")), "db配置");
    properties.storeToXML(new FileOutputStream(new File("e:/others/db.xml")), "db配置");
    properties.load(new FileReader("e:/others/db.properties"));
    System.out.println(properties.getProperty("user","Lili"));

 

转载于:https://www.cnblogs.com/tiancy/p/6018695.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值