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

时间:2022-11-28 06:51:19

一、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"));