Hashtable与Properties_绝对、相对、类路径存储与读取JAVA131

来源:互联网 发布:手机版探鱼器软件 编辑:程序博客网 时间:2024/05/10 18:05

来源:http://www.bjsxt.com/
1、S02E131_01Hashtable与Properties_绝对、相对、类路径存储与读取
(1)Hashtable与HashMap的区别
——a.主要:Hashtable线程安全,同步,效率相对低下;HashMap线程不安全,非同步,效率相对高
——b.父类:Hashtable是Dictionary;HashMap是AbstractMap
——c.null:Hashtable键与值不能为null;HashMap键最多一个null,值可以多个null
(2)Properties(Hashtable子类)
——a.作用:读写资源配置文件
——b.键与值只能为字符串
——c.方法
————setProperty(String key, String value):调用 Hashtable 的方法 put。
————getProperty(String key):用指定的键在此属性列表中搜索属性。
————getProperty(String key, String defaultValue):用指定的键在属性列表中搜索属性。

——后缀:.properties
————store(OutputStream out, String comments):以适合使用 load(InputStream) 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。
————store(Writer writer, String comments):以适合使用 load(Reader) 方法的格式,将此 Properties 表中的属性列表(键和元素对)写入输出字符。
————load(InputStream inStream):从输入流中读取属性列表(键和元素对)。
————load(Reader reader):按简单的面向行的格式从输入字符流中读取属性列表(键和元素对)。
——后缀:.xml
————storeToXML(OutputStream os, String comment):发出一个表示此表中包含的所有属性的 XML 文档。
————storeToXML(OutputStream os, String comment, String encoding):使用指定的编码发出一个表示此表中包含的所有属性的 XML 文档。
————loadFromXML(InputStream in):将指定输入流中由 XML 文档所表示的所有属性加载到此属性表中。
(3)相对路径与绝对路径
a.绝对路径:含盘符:(windows),其它系统使用/
b.相对路径:相对于当前项目或工程
(4)类路径加载资源文件
类所在的根路径(此处指bin)
a.类.class.getResourceAsStream(“/”)); “/”指bin
b.Thread.currentThread().getContextClassLoader().getResourceAsStream(“”); “”指bin

package com.test.properties;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileReader;import java.io.IOException;import java.util.Properties;/** * Properties资源配置文件的读写 * 1.key与value只能为字符串 */public class TestProperties {    public static void main(String[] args) throws FileNotFoundException, IOException {        //创建对象        Properties pro = new Properties();        //存储        //setProperty(String key, String value):调用 Hashtable 的方法 put。        pro.setProperty("driver", "oracle.jdbc.driver.OracleDriver");        pro.setProperty("url", "jdbc:oracle:thin:@localhost:1521:orcl");        pro.setProperty("user", "scott");        pro.setProperty("pwd", "tiger");        test1(pro);        test2(pro);        test3();        test4();    }    /**     * 存储与读取     */    public static void test1(Properties pro){        //读取        //getProperty(String key, String defaultValue):用指定的键在属性列表中搜索属性。        String url = pro.getProperty("url","if value = null then value = defaultValue");        System.out.println(url);    }    /**     * 使用Properties输出到文件     * <br>资源配置文件     * @throws IOException      * @throws FileNotFoundException      */    public static void test2(Properties pro) throws FileNotFoundException, IOException{        //存储到g:/others——存在盘符:的是绝对路径         pro.store(new FileOutputStream(new File                ("g:/others/test2.properties")), "test2配置说明,存储为.properties");        pro.storeToXML(new FileOutputStream(new File                ("g:/others/test2.xml")), "test2配置说明,存储为.xml");        //使用相对路径,存在当前工程(JRE Systme Library目录下)        pro.store(new FileOutputStream(new File                ("test2.properties")), "test2配置说明,JRE Systme Library目录下");        pro.store(new FileOutputStream(new File                ("src/test2.properties")), "test2配置说明,src目录下");        pro.store(new FileOutputStream(new File                ("src/com/test/properties/test2.properties")),                 "test2配置说明,src/com.test.properties目录下");    }    /**     * 使用Properties读取配置文件     * @throws IOException      * @throws FileNotFoundException      */    public static void test3() throws FileNotFoundException, IOException{        Properties pro = new Properties();        //读取绝对路径        pro.load(new FileReader("g:/others/test2.properties"));        System.out.println("test3绝对路径:" +         pro.getProperty("pwd", "if values = null then values = defaultValue"));        //读取当前工程下的相对路径        pro.load(new FileReader("src/com/test/properties/test2.properties"));        System.out.println("test3相对路径:" +         pro.getProperty("pwd", "if values = null then values = defaultValue"));    }    /**     * 使用类相对路径读取配置文件(类所在的根路径,此处根路径指bin)     * @throws IOException      */    public static void test4() throws IOException{        Properties pro = new Properties();        //类点的类相对路径的第一个"/",相当于bin        pro.load(TestProperties.class                .getResourceAsStream("/com/test/properties/test2.properties"));        System.out.println("test4类点的类相对路径:" +         pro.getProperty("pwd", "if values = null then values = defaultValue"));        //类加载器的类相对路径的第一个"",相当于bin        pro.load(Thread.currentThread().getContextClassLoader()                .getResourceAsStream("com/test/properties/test2.properties"));        System.out.println("test4类加载器的类相对路径:" +         pro.getProperty("pwd", "if values = null then values = defaultValue"));    }}
0 0