Java -- Hashtable 和 Properties

来源:互联网 发布:电脑监控软件免费版 编辑:程序博客网 时间:2024/05/14 13:40
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"));    }}
原创粉丝点击