一、Properties 类概述

     1、Properties 类是 Hashtable 的子类,该对象用于处理属性文件;

  2、由于属性文件里的 key、value 都是字符串类型,所以 Properties 里的 key和 value 都是字符串类型;

  3、存取数据时,建议使用setProperty(String key,String value)方法和getProperty(String key)方法;

  4、Properties集合是一个唯一和IO流相结合的集合;

  5、

二、Properties 类结构

  1、Properties 类继承结构

  

  2、Properties 类签名

public class Properties extends Hashtable<Object,Object> {}

  

三、Properties 方法

  1、Properties 方法列表

    

 

  2、常用方法

    (1)构造方法

Properties():创建一个无默认值的空属性列表。
Properties(Properties defaults):创建一个带有指定默认值的空属性列表

  

    (2)获取属性方法

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

  

    (3)设置属性方法

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

  

    (4)其他方法

void load(InputStream inStream):从输入流中读取属性列表(键和元素对)。
void load(Reader reader) :按简单的面向行的格式从输入字符流中读取属性列表(键和元素对)。

  

  3、

四、案例

  1、案例一

 1     @Test
 2     public void test1(){
 3         Properties pro = new Properties();
 4         pro.setProperty("user", "root");
 5         pro.setProperty("pwd", "123456");
 6 
 7         String user = pro.getProperty("user");
 8         String password = pro.getProperty("pwd");
 9         System.out.println(user);
10         System.out.println(password);
11     }

 

  2、案例二

 1     @Test
 2     public void test2() throws IOException{
 3         Properties pro = new Properties();
 4         pro.load(TestMapImpl.class.getClassLoader().getResourceAsStream("jdbc.properties"));
 5 
 6         String user = pro.getProperty("user");
 7         String password = pro.getProperty("password");
 8         System.out.println(user);
 9         System.out.println(password);
10     }

 

  3、案例三

1     @Test
2     public void test3() throws IOException{
3         Properties pro = System.getProperties();//获取系统属性配置
4         Set entrySet = pro.entrySet();
5         for (Object entry : entrySet) {
6             System.out.println(entry);
7         }
8     }

 

  4、案例四

    (1)在工程下创建 jdbc.properties 文件

      

 

 

       属性和值需要顶着等号写,不能写空格。

    (2)根据属性获取值

 1     //Properties:常用来处理配置文件。key和value都是String类型
 2     public static void main(String[] args)  {
 3         FileInputStream fis = null;
 4         try {
 5             Properties pros = new Properties();
 6 
 7             fis = new FileInputStream("jdbc.properties");
 8             pros.load(fis);//加载流对应的文件
 9 
10             String name = pros.getProperty("username");
11             String password = pros.getProperty("password");
12 
13             System.out.println("username = " + name + ", password = " + password);
14         } catch (IOException e) {
15             e.printStackTrace();
16         } finally {
17             if(fis != null){
18                 try {
19                     fis.close();
20                 } catch (IOException e) {
21                     e.printStackTrace();
22                 }
23 
24             }
25         }
26 
27     }

 

五、

 

posted on 2021-05-10 13:16  格物致知_Tony  阅读(133)  评论(0编辑  收藏  举报