java基础--properties 类使用

一、Java Properties类

    Java中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的配置文件,各种语言都有自己所支持的配置文件,配置文件中很多变量是经常改变的,这样做也是为了方便用户,让用户能够脱离程序本身去修改相关的变量设置。像Python支持的配置文件是.ini文件,同样,它也有自己读取配置文件的类ConfigParse,方便程序员或用户通过该类的方法来修改.ini配置文件。在Java中,其配置文件常为.properties文件,格式为文本文件,文件的内容的格式是“键=值”的格式,文本注释信息可以用"#"来注释。

Properties类继承自Hashtable,如下:

它提供了几个主要的方法:

1. getProperty ( String key),用指定的键在此属性列表中搜索属性。也就是通过参数 key ,得到 key 所对应的 value。

2. load ( InputStream inStream),从输入流中读取属性列表(键和元素对)。通过对指定的文件(比如说上面的 test.properties 文件)进行装载来获取该文件中的所有键 - 值对。以供 getProperty ( String key) 来搜索。

3. setProperty ( String key, String value) ,调用 Hashtable 的方法 put 。他通过调用基类的put方法来设置 键 - 值对。

4. store ( OutputStream out, String comments),以适合使用 load 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。与 load 方法相反,该方法将键 - 值对写入到指定的文件中去。

5. clear (),清除所有装载的 键 - 值对。该方法在基类中提供。

二、Hashtable 与HashMap的区别 
1、主要:Hashtable线程安全,同步,效率相对低下
HashMap 线程不安全,非同步,效率相对高
2、父类:Hashtable 是 Dictionary HashMap 是 AbstractMap
3、null: Hashtable键与值不能为null
HashMap 键最多一个null,值可以多个null
三、Properties
     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(Reader 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("")

package com.bjsxt.others.pro;

import java.util.Properties;

/**
 * Properties 资源配置文件的读写
 * 1、key 与value 只能为字符串
 * 2、存储与读取
 * setProperty(String key, String value) 
 * getProperty(String key, String defaultValue)  
 * @author Administrator
 *
 */
public class Demo01 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        //创建对象
        Properties pro =new Properties();
        //存储
        pro.setProperty("driver", "oracle.jdbc.driver.OracleDriver");
        //pro.setProperty("url", "jdbc:oracle:thin:@localhost:1521:orcl");
        pro.setProperty("user", "scott");
        pro.setProperty("pwd", "tiger");
        
        //获取
        String url =pro.getProperty("url","test");
        System.out.println(url);
    }

}
demo1
package com.bjsxt.others.pro;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * 使用Properties 输出到文件
 * 资源配置文件:
 * 
 * 1、.properties
 * store(OutputStream out, String comments) 
    store(Writer writer, String comments) 
   2、.xml
   storeToXML(OutputStream os, String comment)  :UTF-8字符集
   storeToXML(OutputStream os, String comment, String encoding) 
    

 * @author Administrator
 *
 */
public class Demo02 {

    /**
     * @param args
     * @throws IOException 
     * @throws FileNotFoundException 
     */
    public static void main(String[] args) throws FileNotFoundException, IOException {
        //创建对象
        Properties pro =new Properties();
        //存储
        pro.setProperty("driver", "oracle.jdbc.driver.OracleDriver");
        pro.setProperty("url", "jdbc:oracle:thin:@localhost:1521:orcl");
        pro.setProperty("user", "scott");
        pro.setProperty("pwd", "tiger");
        
        //存储到e:/others  绝对路径  盘符:
        //pro.store(new FileOutputStream(new File("e:/others/db.properties")), "db配置");
        //pro.storeToXML(new FileOutputStream(new File("e:/others/db.xml")), "db配置");
        //使用相对路径 当前的工程
//        pro.store(new FileOutputStream(new File("db.properties")), "db配置");
//        pro.store(new FileOutputStream(new File("src/db.properties")), "db配置");
        pro.store(new FileOutputStream(new File("src/com/bjsxt/others/pro/db.properties")), "db配置");
    }

}
demo2
package com.bjsxt.others.pro;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

/**
 * 使用Properties读取配置文件
 * 资源配置文件:
 * 使用相对与绝对路径读取
 * load(InputStream inStream) 
   load(Reader reader) 
   loadFromXML(InputStream in) 
 * @author Administrator
 *
 */
public class Demo03 {

    /**
     * @param args
     * @throws IOException 
     * @throws FileNotFoundException 
     */
    public static void main(String[] args) throws FileNotFoundException, IOException {
        Properties pro=new Properties();
        //读取 绝对路径
        //pro.load(new FileReader("e:/others/db.properties"));
        //读取 相对路径
        pro.load(new FileReader("src/com/bjsxt/others/pro/db.properties"));
        System.out.println(pro.getProperty("user", "bjsxt"));
    }

}
demo3
package com.bjsxt.others.pro;

import java.io.IOException;
import java.util.Properties;

/**
 * 使用类相对路径读取配置文件
 *  bin  
 * @author Administrator
 *
 */
public class Demo04 {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        Properties pro =new Properties();
        //类相对路径的 / bin 
        //pro.load(Demo04.class.getResourceAsStream("/com/bjsxt/others/pro/db.properties"));
        //"" bin 
        pro.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("com/bjsxt/others/pro/db.properties"));
        System.out.println(pro.getProperty("user", "bjsxt"));
    }

}
demo4
原文地址:https://www.cnblogs.com/ou-pc/p/7668079.html