加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java_Properties

发布时间:2020-12-14 06:26:26 所属栏目:Java 来源:网络整理
导读:Java_Properties类 Hashtable与HashMap区别 主要:Hashtable线程安全,同步,效率相对低下 HashMap线程不安全,异步,效率高 父类:Hashtable父类是Dictionary HashMap父类是AbstractMap Hashtable无论键值都不能为null HashMap键可以有一个为空 ?????????????????

Java_Properties类

Hashtable与HashMap区别

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

    HashMap线程不安全,异步,效率高

  1. 父类:Hashtable父类是Dictionary

    HashMap父类是AbstractMap

  1. Hashtable无论键值都不能为null

    HashMap键可以有一个为空

??????????????????? 值可以有很多为null

    Properties继承于Hashtable

Properties类常用方法:

String getProperty(String key,String defaultValue)
用指定的键在属性列表中搜索属性.

Object setProperty(String key,String value)
调用 Hashtable 的方法 put。

<span style="color: #0000ff">void<span style="color: #000000"> store(OutputStream out,String comments)
以适合使用 load(InputStream) 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。

<span style="color: #0000ff">void<span style="color: #000000"> storeToXML(OutputStream os,String comment)
发出一个表示此表中包含的所有属性的 XML 文档。默认编码UTF-8

<span style="color: #0000ff">void<span style="color: #000000"> storeToXML(OutputStream os,String comment,String encoding)
使用指定的编码发出一个表示此表中包含的所有属性的 XML 文档。

Set<span style="color: #000000"> stringPropertyNames()
返回此属性列表中的键集,其中该键及其对应值是字符串,如果在主属性列表中未找到同名的键,则还包括默认属性列表中不同的键。

Enumeration<?><span style="color: #000000"> propertyNames()
返回属性列表中所有键的枚举,如果在主属性列表中未找到同名的键,则包括默认属性列表中不同的键。

四种加载资源文件方法

直接加载

    1.相对路径   

p.load( FileInputStream( File("db.properties")));

    2.绝对路径

使用类的相对路径获取资源文件

    3.类路径加载资源文件1  

p.load(Demo3..getResourceAsStream("/com/ahd/Hashtable/db.properties"));;

    4.类路径加载资源文件2  

    p.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("com/ahd/Hashtable/db.properties"));;

?练习代码

<span style="color: #0000ff">import<span style="color: #000000"> java.util.Properties;

<span style="color: #008000">/*<span style="color: #008000">

  • Hashtable与HashMap区别:

  • 1.Hashtable线程安全,效率低下

  • HashMap线程不安全,效率高

  • 2.继承的父类不同,Hashtable继承于Dictionary

  • HashMap继承于AbstractMap

  • 3.Hashtable无论键值不能为null

  • HashMap键可以有一个为null,值可以有很多null

  • properties是Hashtable的子类

  • <span style="color: #808080">@author<span style="color: #008000"> Administrator

  • <span style="color: #008000">*/
    <span style="color: #0000ff">public <span style="color: #0000ff">class<span style="color: #000000"> Demo1 {
    <span style="color: #0000ff">public <span style="color: #0000ff">static <span style="color: #0000ff">void<span style="color: #000000"> main(String[] args) {
    Properties p=<span style="color: #0000ff">new<span style="color: #000000"> Properties();
    <span style="color: #008000">//<span style="color: #008000">设置存储值
    p.setProperty("user","scott"<span style="color: #000000">);
    p.setProperty("password","123456"<span style="color: #000000">);
    <span style="color: #008000">//<span style="color: #008000">读取值
    String user=p.getProperty("user"<span style="color: #000000">);
    String password=p.getProperty("password","test"<span style="color: #000000">);

     System.out.println(user);
     System.out.println(password);

    }
    }

<div class="cnblogs_code">  <img id="code_img_closed_e9e4967e-0434-4626-8e69-2d2b591b06d9" class="code_img_closed" src="https://www.52php.cn/res/2019/02-08/14/1c53668bcee393edac0d7b3b3daff1ae.gif" alt=""><img id="code_img_opened_e9e4967e-0434-4626-8e69-2d2b591b06d9" class="code_img_opened" style="display: none" src="https://www.52php.cn/res/2019/02-08/14/405b18b4b6584ae338e0f6ecaf736533.gif" alt=""><div id="cnblogs_code_open_e9e4967e-0434-4626-8e69-2d2b591b06d9" class="cnblogs_code_hide">

<span style="color: #0000ff">import<span style="color: #000000"> java.io.File;
<span style="color: #0000ff">import<span style="color: #000000"> java.io.FileNotFoundException;
<span style="color: #0000ff">import<span style="color: #000000"> java.io.FileOutputStream;
<span style="color: #0000ff">import<span style="color: #000000"> java.io.IOException;
<span style="color: #0000ff">import<span style="color: #000000"> java.util.Properties;

<span style="color: #0000ff">public <span style="color: #0000ff">class<span style="color: #000000"> Demo2 {
<span style="color: #0000ff">public <span style="color: #0000ff">static <span style="color: #0000ff">void main(String[] args) <span style="color: #0000ff">throws<span style="color: #000000"> FileNotFoundException,IOException {
<span style="color: #008000">//<span style="color: #008000">设置键值
Properties p=<span style="color: #0000ff">new<span style="color: #000000"> Properties();
p.setProperty("user","123456"<span style="color: #000000">);

    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;存入文件 .properties 绝对路径</span>
    p.store(<span style="color: #0000ff"&gt;new</span> FileOutputStream(<span style="color: #0000ff"&gt;new</span> File("C:UsersAdministratorDesktopdb.properties")),"db配置备注"<span style="color: #000000"&gt;);
    p.storeToXML(</span><span style="color: #0000ff"&gt;new</span> FileOutputStream(<span style="color: #0000ff"&gt;new</span> File("C:UsersAdministratorDesktopdb.xml")),"db配置备注"<span style="color: #000000"&gt;);

    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;存入文件,相对路径 默认是本工程</span>
    p.store(<span style="color: #0000ff"&gt;new</span> FileOutputStream(<span style="color: #0000ff"&gt;new</span> File("db.properties")),"db配置备注"<span style="color: #000000"&gt;);
    p.storeToXML(</span><span style="color: #0000ff"&gt;new</span> FileOutputStream(<span style="color: #0000ff"&gt;new</span> File("db.xml")),"db配置备注"<span style="color: #000000"&gt;);

}

}

<div class="cnblogs_code">  <img id="code_img_closed_e7fd1d23-0a76-46a2-a7df-c018d8c3851f" class="code_img_closed" src="https://www.52php.cn/res/2019/02-08/14/1c53668bcee393edac0d7b3b3daff1ae.gif" alt=""><img id="code_img_opened_e7fd1d23-0a76-46a2-a7df-c018d8c3851f" class="code_img_opened" style="display: none" src="https://www.52php.cn/res/2019/02-08/14/405b18b4b6584ae338e0f6ecaf736533.gif" alt=""><div id="cnblogs_code_open_e7fd1d23-0a76-46a2-a7df-c018d8c3851f" class="cnblogs_code_hide">

<span style="color: #0000ff">import<span style="color: #000000"> java.io.File;
<span style="color: #0000ff">import<span style="color: #000000"> java.io.FileInputStream;
<span style="color: #0000ff">import<span style="color: #000000"> java.io.FileNotFoundException;
<span style="color: #0000ff">import<span style="color: #000000"> java.io.IOException;
<span style="color: #0000ff">import<span style="color: #000000"> java.io.InputStream;
<span style="color: #0000ff">import<span style="color: #000000"> java.util.Properties;

<span style="color: #008000">/*<span style="color: #008000">

  • 从文件中读取map信息

  • <span style="color: #808080">@author<span style="color: #008000"> Administrator

  • <span style="color: #008000">*/
    <span style="color: #0000ff">public <span style="color: #0000ff">class<span style="color: #000000"> Demo3 {
    <span style="color: #0000ff">public <span style="color: #0000ff">static <span style="color: #0000ff">void main(String[] args) <span style="color: #0000ff">throws<span style="color: #000000"> FileNotFoundException,IOException {
    Properties p=<span style="color: #0000ff">new<span style="color: #000000"> Properties();
    <span style="color: #008000">//<span style="color: #008000">相对路径
    <span style="color: #008000">//<span style="color: #008000">p.load(new FileInputStream(new File("db.properties")));

     </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;绝对路径
     </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;p.load(new FileInputStream(new File("C:UsersAdministratorDesktopdb.properties")));
    
     </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;类路径加载资源文件
     </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;p.load(Demo3.class.getResourceAsStream("/com/ahd/Hashtable/db.properties"));;
    
     </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;线程加载</span>
     p.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("com/ahd/Hashtable/db.properties"<span style="color: #000000"&gt;));;
     System.out.println(p.getProperty(</span>"user","ddd"<span style="color: #000000"&gt;));

    }
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读