Java HashTable 以及子类 Properties使用

来源:互联网 发布:安卓hd软件 编辑:程序博客网 时间:2024/06/05 19:08

hashTable


  • 同样是MAP的实现类 , 与HashMap使用方法相同

继承体系

HashTable :public class Hashtable<K,V>extends Dictionary<K,V>implements Map<K,V>, Cloneable, Serializable



HashMap :public class HashMap<K,V>extends AbstractMap<K,V>implements Map<K,V>, Cloneable, Serializable


差异(面试):

  1. hashTable 线程安全 : 效率低下 , HashMap 线程不安全 : 效率较高
  2. 父类不同 :HashTable extends Dictionary ; HashMap extends AbstractMap;
  3. HashTable键值不能为null ——– HashMap键值最多一个为null,值可以多个是null;

继承子类: Properties

  • 读写资源配置文件
  • 键与值都只能是String
  • 方法:
    • setProperty(String key,String value);
    • getProperty(String key);
    • getProperty(String key,String default);

配置文件 :

  • 后缀为 properties 类型的

    返回值 说明 void load(InputStream inStream)从输入流中读取属性列表(键和元素对)。 void load(Reader reader)按简单的面向行的格式从输入字符流中读取属性列表(键和元素对)。 void store(OutputStream out, String comments)以适合使用 load(InputStream) 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。 void store(Writer writer, String comments)以适合使用 load(Reader) 方法的格式,将此 Properties 表中的属性列表(键和元素对)写入输出字符。




  • 后缀为 XML 类型
返回值 说明 void loadFromXML(InputStream in) 将指定输入流中由 XML 文档所表示的所有属性加载到此属性表中。 void storeToXML(OutputStream os, String comment, String encoding)使用指定的编码发出一个表示此表中包含的所有属性的 XML 文档.

代码演示

演示代码如下

先创建配置信息

    package club.dohacker.Test;    import java.io.File;    import java.io.FileNotFoundException;    import java.io.FileOutputStream;    import java.io.IOException;    import java.util.Properties;    /**     * properties 资源配置文件的读写     * 注意 :  ptrperties 对象的key和value只能存储String      * @author Administrator     *     */    public class Test1 {        public static void main(String[] args) throws FileNotFoundException, IOException {          //创建对象          Properties pro = new Properties();            //存储数据          pro.setProperty("Driver","oracle.jdbc.driver.OracleDriver");     //存放oracle数据库的驱动          pro.setProperty("url","jdbc:oracle:thin@ localhost:1521:orcl");            pro.setProperty("user","zc");          pro.setProperty("password", "123456");          //将信息写入文件          //写入properties 文件当中          pro.store(new FileOutputStream(new File("1.properties")),"db配置文件");          //写入XML文件          pro.store(new FileOutputStream(new File("2.xml")), "db配置文件");        }    }

在读取文件

    package club.dohacker.Test;    import java.io.File;    import java.io.FileInputStream;    import java.io.FileNotFoundException;    import java.io.IOException;    import java.util.Properties;    public class Test2 {      public static void main(String[] args) throws FileNotFoundException, IOException {          Properties pro = new Properties();          //加载配置文件(properties);          pro.load(new FileInputStream(new File("1.properties")));          //从已经创建的properties文件中获取数据          System.out.println(pro.getProperty("user","asd"));      }    }

根据类路径加载配置文件

  1. 类.class.getResourceAsStream(“/”); // /表示src文件
  2. Thread.currentThread().getContextClassLoader().getResourceAsStream(); // 空表示src文件下的

代码演示如下 :

    package club.dohacker.Test;    import java.io.File;    import java.io.FileInputStream;    import java.io.FileNotFoundException;    import java.io.IOException;    import java.util.Properties;    public class Test2 {      public static void main(String[] args) throws FileNotFoundException, IOException {          Properties pro = new Properties();          //加载配置文件(properties);          //pro.load(new FileInputStream(new File("1.properties")));          //使用类加载          //pro.load(Test2.class.getResourceAsStream("/club/dohacker/Test/1.properties"));          Thread.currentThread().getContextClassLoader().getResourceAsStream("club/dohacker/Test/1.properties");          //从已经创建的properties文件中获取数据          System.out.println(pro.getProperty("user"));          System.out.println();  }}