Java_ Properties

Java_ Properties class

Differences between hashtable and HashMap

HashMap thread is unsafe, asynchronous and efficient

The HashMap parent class is abstractmap

One of the HashMap keys can be empty

Many values can be null

Properties inherit from hashtable

Common methods of properties class:

Four methods of loading resource files

Direct loading

    1. Relative path

    2. Absolute path

Get the resource file using the relative path of the class

    3. Classpath load resource file 1

    4. Classpath load resource file 2

Practice code

  

<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;存入<a href="https://www.jb51.cc/tag/wenjian/" target="_blank" class="keywords">文件</a> .properties <a href="https://www.jb51.cc/tag/jueduilujing/" target="_blank" class="keywords">绝对路径</a></span>
    p.store(<span style="color: #0000ff"&gt;new</span> FileOutputStream(<span style="color: #0000ff"&gt;new</span> File("C:\\Users\\Administrator\\Desktop\\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("C:\\Users\\Administrator\\Desktop\\db.xml")),"db配置备注"<span style="color: #000000"&gt;);

    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;存入<a href="https://www.jb51.cc/tag/wenjian/" target="_blank" class="keywords">文件</a>,相对路径 <a href="https://www.jb51.cc/tag/mo/" target="_blank" class="keywords">默</a>认是本工程</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;);

}

}

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>