Map的实现类,与HashMap用法相同
==区别==:
1.Hashtable线程安全,同步,效率低;HashMap线程不安全,非同步,效率高;
2.父类:Hashtable-Dictionary; HashMap-AbstractMap
3.键值:Hashtable键与值不能为null;HashMap键最多一个null,值可以多个null;
==Hashtable子类==:Properties
1.读写资源配置文件
2.键值只能为String
3.方法:
setProperty(String key, String value)
getProperty(String key):使用键搜索值,不存在返回空
getProperty(String key, String defaultValue):不存在返回defaultValue
store(OutputStream out, String comments):字节流
store(Writer writer, String comments):字符流
load
storeToXML(OutputStream os, String comment):发出表示此表中包含的所有属性的XML文档(默认UTF-8)
storeToXML(OutputStream os, String comment, String encoding):使用指定的编码发出表示此表中包含的所有属性的XML文档
package com.alexanderli95.otherCollections.pro;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
/**
* Properties资源配置文件的读写
*/
public class Demo01 {
public static void main(String[] args) throws IOException {
Properties pro=new Properties();
//存储
pro.setProperty("driver","oracle.jdbc.driver.OracleDriver");
pro.setProperty("url","jdbc:oracle.thin:@localhost:1521:orcl");
pro.setProperty("user","scoll");
pro.setProperty("psw","tiger");
//获取
String str=pro.getProperty("url","N/A");
System.out.println(str);
//输出到资源配置文件
pro.store(new FileOutputStream(new File("e:/db.properties")),"dbConfig");
pro.store(new FileOutputStream(new File("e:/db.xml")),"dbConfig");
//输出到工程文件夹中(相对路径)
pro.store(new FileOutputStream(new File("db.xml")),"dbConfig");
}
}
package com.alexanderli95.otherCollections.pro;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
/**
* properties读取
*/
public class Demo03 {
public static void main(String[] args) throws IOException {
Properties pro=new Properties();
pro.load(new FileReader("db.xml"));
System.out.println(pro.getProperty("user","N/A"));
}
}
package com.alexanderli95.otherCollections.pro;
import java.io.IOException;
import java.util.Properties;
public class Demo04 {
public static void main(String[] args) throws IOException {
Properties pro=new Properties();
pro.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("")); //工程中常用 ""表示bin目录
pro.load(Demo04.class.getResourceAsStream("/db.xml"));
}
}