Java -- Hashtable 和 Properties

Java -- Hashtable 和 Properties
一.Hashtable和HashMap
  (1)基本操作是一致的,主要关注不同点
  (2)差别:
      ①主要:Hashtable线程安全、同步,效率相对低
            HashMap线程不安全,非同步,效率相对较高
      ②父类:Hashtable是继承的Dictionary
             HashMap是继承的AbstractMap
      ③null:Hashtable的键与值都不能null
             HashMap键最多一个null,值可以有多个null
二.Properties
   0.特点:是Hashtable的子类
   1.作用:读写资源配置文件
   2.键与值都只能字符串
   3.方法
       存储:setProperty(String key, String value)
       获取:getProperty(String key)
            getProperty(String key, String defaultValue)
            第二个get:如果获取到了key,则返回key的value;如果没有,则返回defaultValue
       输出到/加载文件:
        1.输出到/加载后缀为.properties的文件
          store(OutputStream out, String comments)
          store(Writer writer, String comments)
          load(InputStream inStream)
          laod(Reader reader)
        2.输出到/加载后缀为.xml的文件
          storeToXML(OutputStream os, String comment)
          storeToXML(OutputStream os, String comment,String encoding)
          loadFromXML(InputStream in)

三.绝对路径和相对路径
   1.绝对路径:windows下是盘符;linux下是/
   2.相对路径:当前工程、项目下

四.类路径加载资源文件 
  类所在的根路径
  1、类.class.getResourceAsStream("/")#这就是bin的路径
  2、Thread.currentTread().getContextClassLoader().getResourceAsStream("")#空,即bin的路径
  代码如下
/**
 * Properties文件的读取
 * 方法:setProperty(String key, String value)
 *      getProperty(String key, String defaultValue)
 */
public class Demo01 {
    public static void main(String[] args) {
        Properties properties = new Properties();
        //存储
        properties.setProperty("driver", "oracle.jdbc.driver.OracleDriver");
        properties.setProperty("url", "jdbc:oracle:thin:@localhost:1521:orcl");
        properties.setProperty("user", "scott");
        properties.setProperty("pwd", "000010");
        //获得
        System.out.println(properties.getProperty("url","abcde"));


    }
}
/**
 * 使用Properties输出到文件
 * store(OutputStream out, String comments)
   store(Writer writer, String comments)
   storeToXML(OutputStream os, String comment)
   storeToXML(OutputStream os, String comment,String encoding)
 */
public class Demo02 {
    public static void main(String[] args) throws IOException{
        //建立一个对象
        Properties properties = new Properties();
        //存储
        properties.setProperty("driver", "oracle.jdbc.driver.OracleDriver");
        properties.setProperty("url", "jdbc:oracle:thin:@localhost:1521:orcl");
        properties.setProperty("user", "scott");
        properties.setProperty("pwd", "000010");
        //输出到文件
        //先是绝对地址
        properties.store(new FileOutputStream(new File("g:/db.properties")),"db配置");
        properties.storeToXML(new FileOutputStream(new File("g:/db.xml")),"db配置");

        //后是相对寻址
        properties.store(new FileOutputStream(new File("db.properties")),"db配置");
        properties.store(new FileOutputStream(new File("src/db.properties")),"db配置");
        properties.storeToXML(new FileOutputStream(new File("db.xml")),"db配置");







    }
}
/**
 * 使用Property读取配置文件
 * load(InputStream inStream)
    load(Reader reader)
    loadFromXML(InputStream in)
 *
 */
public class Demo03 {
    public static void main(String[] args) throws IOException {
        //建立一个对象
        Properties properties = new Properties();

        //读取配置文件
        //先是绝对地址
//        properties.load(new FileReader(new File("G:/db.properties")));
//        System.out.println(properties.getProperty("url"));
        //再是相对地址
        properties.load(new FileReader("src/db.properties"));
        System.out.println(properties.getProperty("url"));



    }
}
   此时注意,读取的配置文件是在src里的,但是我们给客户的文件是在bin文件或者其他文件里的,那这时怎么办呢,就要用到上文提到的类路径加载资源文件
   在代码的示例中,我们是用的bin,bin也可以替换成其他别的文件名       
/**
 * 使用类相对路径读取配置文件
 * bin
 * Created by chengyong on 2017/5/18.
 */
public class Demo04 {
    public static void main(String[] args) throws IOException {
        Properties properties = new Properties();

//        properties.load(Demo04.class.getResourceAsStream("/db.properties"));//此种语法下,/即代表着bin/
//        System.out.println(properties.getProperty("url"));
        properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties"));//此种语法下,""即代表着bin/
        System.out.println(properties.getProperty("url"));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值