Hashtable与Properties

来源:互联网 发布:数据挖掘的基本算法 编辑:程序博客网 时间:2024/05/29 03:31

HashMap和Hashtable的区别如下:

  1. Hashtable线程安全,同步,效率相对低下。而HashMap线程不安全,非同步,效率相对较高。
  2. Hashtable的父类是Dictionary。HashMap的父类是AbstractMap
  3. null:Hashtable键与值不能为null。HashMap键最多一个null,值可以多个null

Properties的作用是读写资源配置文件,它要求键与值只能为字符串
它的方法:

  • setProperty(String key,String value)–调用 Hashtable 的方法 put–用指定的键在属性列表中搜索属性。
  • getProperty(String key)– 用指定的键在此属性列表中搜索属性。
  • store(OutputStream out, String comments)/store(Writer writer, String comments) –存储为后缀:.properties
  • storeToXML(OutputStream os, String comment)(默认字符集:UTF-8)/storeToXML(OutputStream os, String comment, String encoding)(可以指定字符集)–存储为后缀:.xml

运用:

    import java.util.Properties;/** * Properties资源配置文件的读写 * 只能存储字符串--所有key和values只能是字符串 * setProperty(String key,String value) * getProperty(String key,String defaultValue) * @author Administrator * */public class Demoo3 {    public static void main(String[] args) {        //创建对象        Properties pro=new Properties();        //存储        pro.setProperty("driver", "oracle.jdbc.driver.OracleDriver");        pro.setProperty("url", "jdbc:oracle:thin:@loaclhost:1521:orcl");        pro.setProperty("user", "scott");        pro.setProperty("pwd", "tiger");        //获取        String url=pro.getProperty("url", "test");        System.out.println(url);    }}

输出:

jdbc:oracle:thin:@loaclhost:1521:orcl

配置文件:

    import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.util.Properties;/** * 使用Properties输出到文件 * 资源配置文件:可以动态的切换数据库 * store(OutputStream out, String comments) * store(Writer writer, String comments) * storeToXML(OutputStream os, String comment)(默认字符集:UTF-8) * storeToXML(OutputStream os, String comment, String encoding) * @author Administrator * */public class Demoo4 {    public static void main(String[] args) throws FileNotFoundException, IOException {        //创建对象        Properties pro=new Properties();        //存储        pro.setProperty("driver", "oracle.jdbc.driver.OracleDriver");        pro.setProperty("url", "jdbc:oracle:thin:@loaclhost:1521:orcl");        pro.setProperty("user", "scott");        pro.setProperty("pwd", "tiger");        //存储到e:/others 绝对路径 盘符        pro.store(new FileOutputStream(new File("e:/others/db.properties")), "db配置");        pro.storeToXML(new FileOutputStream(new File("e:/others/db.xml")), "db配置");        //使用相对路径 当前的工程        pro.store(new FileOutputStream(new File("db.properties")), "db配置");        pro.storeToXML(new FileOutputStream(new File("src/db.xml")), "db配置");    }}

读取配置文件:

    import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.util.Properties;/** * 使用Properties读取配置文件 * 使用相对与绝对路径 * 方法: * load(InputStream inStream) * load(Reader reader) * loadFromXML(InputStream in) * @author Administrator * */public class Demoo5 {    public static void main(String[] args) throws FileNotFoundException, IOException {        Properties pro=new Properties();        //读取 绝对路径        //pro.load(new FileReader("e:/others/db.properties"));        //读取 相对路径        pro.load(new FileReader("db.properties"));        System.out.println(pro.getProperty("user","ok"));    }}
原创粉丝点击