zoukankan      html  css  js  c++  java
  • Properties简介——高淇JAVA300讲笔记之Hashtable

    一、Hashtable与HashMap的区别

    1、主要:Hashtable线程安全,同步,效率相对低下

          HashMap线程不安全,非同步,效率相对高

    2、父类:Hashtable 是Dictionary  HashMap 是AbstractMap

    3、null:  Hashtable键与值不能为null

           HashMap键最多一个null,值可以多个null


    二、Properties(Hashtable的子类)

    1、作用:读写资源配置文件

    2、键与值只能为字符串

    3、方法:

    setProperty(String key, String value)

    getProperty(String key)

    getProperty(String key, String defaultValue)

    后缀:properties

      store(OutputStream out, String comments)

      store(Writer writer, String comments)

      load(InputStream inStream)

      load(Read reader)

    后缀:.xml

      storeToXML(OutputStream os, String comment)  :UTF-8字符集

      storeToXML(OutputStream os, String comment, String encoding)

      loadFromXML(InputStream in)


    三、相对路径与绝对路径

    1、绝对路径: 盘符:/

    2、相对路径:当前项目、工程


    四、类路径加载资源文件

    类所在的根路径

    1、类.class.getResourceAsStream("/")

    2、Thread.currentThread().getContextClassLoader().getResourceAsStream("")


    案例一:存储与读取

     1 package com.bjsxt.others.pro;
     2 
     3 import java.util.Properties;
     4 
     5 /**
     6  * Properties资源配置文件的读写
     7  * 1、key与value只能为字符串
     8  * 2、存储与读取
     9  * setProperty(String key, String value)
    10  * getProperty(String key, String defaultValue)
    11  *
    12  */
    13 public class Demo01 {
    14     public static void main(String[] args) {
    15         //创建对象
    16         Properties pro = new Properties();
    17         //存储
    18         pro.setProperty("driver", "oracle.jdbc.driver.OracleDriver");
    19         pro.setProperty("url", "jdbc:oracle:thin:@localhost:1521:orcl");
    20         pro.setProperty("user", "scott");
    21         pro.setProperty("pwd", "tiger");
    22         
    23         //获取
    24         String url = pro.getProperty("url","test");  //如果不存在,使用给定的默认值test
    25         System.out.println(url);
    26         
    27     }
    28 }

    运行结果:

    jdbc:oracle:thin:@localhost:1521:orcl

    案例二:使用Properties 输出到文件

     1 package com.bjsxt.others.pro;
     2 
     3 import java.io.File;
     4 import java.io.FileNotFoundException;
     5 import java.io.FileOutputStream;
     6 import java.io.IOException;
     7 import java.util.Properties;
     8 
     9 /**
    10  * 使用Properties 输出到文件
    11  * 资源配置文件:
    12  * 1、.properties
    13  *     store(OutputStream out, String comments)
    14     store(Writer writer, String comments)
    15    2、.xml
    16     storeToXML(OutputStream os, String comment)  :UTF-8字符集
    17    storeToXML(OutputStream os, String comment, String encoding)  
    18  *
    19  */
    20 public class Demo02 {
    21     public static void main(String[] args) throws FileNotFoundException, IOException {
    22         //创建对象
    23         Properties pro = new Properties();
    24         //存储
    25         pro.setProperty("driver", "oracle.jdbc.driver.OracleDriver");
    26         pro.setProperty("url", "jdbc:oracle:thin:@localhost:1521:orcl");
    27         pro.setProperty("user", "scott");
    28         pro.setProperty("pwd", "tiger");
    29         
    30         //存储到e:/other 绝对路径 盘符:
    31         //pro.store(new FileOutputStream(new File("e:/others/db.properties")), "db配置");
    32         //pro.storeToXML(new FileOutputStream(new File("e:/others/db.xml")), "db配置");
    33         //使用相对路径 
    34 //        pro.store(new FileOutputStream(new File("db.properties")), "db配置"); //默认路径是当前的工程
    35 //        pro.store(new FileOutputStream(new File("src/db.properties")), "db配置");
    36         pro.store(new FileOutputStream(new File("src/com/bjsxt/others/pro/db.properties")), "db配置");
    37         
    38         
    39     }
    40 }

    案例三:使用Properties读取配置文件

     1 package com.bjsxt.others.pro;
     2 
     3 import java.io.FileNotFoundException;
     4 import java.io.FileReader;
     5 import java.io.IOException;
     6 import java.util.Properties;
     7 
     8 /**
     9  * 使用Properties读取配置文件
    10  * 资源配置文件:
    11  * 使用相对、绝对路径读取
    12  * load(InputStream inStream)
    13    load(Read reader)
    14    loadFromXML(InputStream in)
    15  */
    16 public class Demo03 {
    17     public static void main(String[] args) throws FileNotFoundException, IOException {
    18         Properties pro = new Properties();
    19         //读取绝对路径
    20 //        pro.load(new FileReader("e:/others/db.properties"));
    21         //读取相对路径
    22         pro.load(new FileReader("src/com/bjsxt/others/pro/db.properties"));
    23         
    24         System.out.println(pro.getProperty("user","bjsxt"));
    25     }
    26 }

    运行结果:

    jdbc:oracle:thin:@localhost:1521:orcl

    案例四:使用类相对路径读取配置文件

     1 package com.bjsxt.others.pro;
     2 
     3 import java.io.IOException;
     4 import java.util.Properties;
     5 
     6 /**
     7  * 使用类相对路径读取配置文件
     8  * bin
     9  * 
    10  */
    11 public class Demo04 {
    12     public static void main(String[] args) throws IOException {
    13         Properties pro = new Properties();
    14         //类相对路径的 '/'表示 bin
    15 //        pro.load(Demo04.class.getResourceAsStream("/com/bjsxt/others/pro/db.properties"));
    16         //""表示bin
    17         pro.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("com/bjsxt/others/pro/db.properties"));
    18         System.out.println(pro.getProperty("user","bjsxt"));
    19     }
    20 }

    运行结果:

    scott
  • 相关阅读:
    url路由配置及渲染方式
    django类视图介绍与类视图装饰器
    什么是数据类型
    python代码的编写和运行
    python环境搭建
    python教程(目录)
    编程语言概念
    面向对象入门
    编程语言
    Tornado框架实现图形验证码功能
  • 原文地址:https://www.cnblogs.com/swimminglover/p/8331774.html
Copyright © 2011-2022 走看看