properties文件解析的几种方式

主要是给自己留个印象,忘了可以再来翻翻。当然这个东西也是借鉴了别人的,
package com.fd.util;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

public class LoadProperties {

private static String Path_properties = "src/com/fd/util/config.properties";//全类名  
private static String Url = null;  
private static String DriverClassName = null;  
private static String User = null;  
private static String Password = null;  
**第一种:**
/**
 * 全路径的文件,全类名是某个文件在项目中的位置,格式为包名.类名
 */
public static void loadProperties1(){  
    //获得properties  
    Properties property = new Properties();//流文件    
    InputStream ins = null;  
    try {  
        ins = new FileInputStream(Path_properties);  
    } catch (FileNotFoundException e) {  
        e.printStackTrace();  
    }  

    try {  
            property.load(ins); 
    } catch (IOException e1) {  
        System.out.println("Properties加载失败");  
        e1.printStackTrace();  
    }  

}  




**第二种:**

/**
 * 包路径,根路径前要加"/"
 */

public static void loadProperties2(){  
    InputStream ins = LoadProperties.class.getResourceAsStream("/com/fd/util/config.properties");  

    Properties property = new Properties();    
    try {    
        property.load(ins);    
    } catch (IOException e) {    
        e.printStackTrace();    
    }    

    printProperties(property);  
}  





**第三种:**
/**
 *  包路径+properties文件名+.后缀
 */

public static void loadProperties3(){  
    InputStream ins = LoadProperties.class.getClassLoader().getResourceAsStream("com/fd/util/config.properties");  

    Properties property = new Properties();    
    try {    
        property.load(ins);    
    } catch (IOException e) {    
        e.printStackTrace();    
    }    

    printProperties(property);  
}  




**第四种:**
/**
 * 包路径+properties文件名+.后缀
 */
public static void loadProperties4(){  
    InputStream ins = ClassLoader.getSystemResourceAsStream("com/fd/util/config.properties");  

    Properties property = new Properties();  
    try {  
        property.load(ins);  
    } catch (IOException e) {  
        e.printStackTrace();  
    }  


}  

第五种:
/**
* 包路径+properties文件名
*/
public static void loadProperties5(){
ResourceBundle rb = ResourceBundle.getBundle(“com/fd/util/config”);

    //rb.getString()方法,参数是properties文件的各属性名  
    System.out.println(rb.getString("mysql.driver"));  
    System.out.println(rb.getString("mysql.url"));  
    System.out.println(rb.getString("mysql.user"));  
    System.out.println(rb.getString("mysql.password"));  
} 



**第六种:**
/**
 *  properties的全类名   在项目中成功 
 *  使用java.util.PropertyResourceBundle类的构造函数     
 */
public void loadProperties6(){  
    InputStream ins = null;  
    ResourceBundle rb = null;   
       try {    
           ins = new BufferedInputStream(new FileInputStream(Path_properties));   

           rb = new PropertyResourceBundle(ins);  

       } catch (FileNotFoundException e) {    
           e.printStackTrace();    
       } catch (IOException e) {    
           e.printStackTrace();    
       }    

   //rb.getString()方法,参数是properties文件的各属性名  
   System.out.println(rb.getString("jdbc.driverClassName"));  
   System.out.println(rb.getString("jdbc.url"));  
   System.out.println(rb.getString("jdbc.username"));  
   System.out.println(rb.getString("jdbc.password"));  

}

/**方法   输出  Properties 的属性 
 * @param property 
 */  
public static void printProperties(Properties property){  
    //连接属性  
    DriverClassName = property.getProperty("mysql.driver");  
    Url = property.getProperty("mysql.url");  
    User = property.getProperty("mysql.user");  
    Password = property.getProperty("mysql.password");  

    System.out.println(DriverClassName+"\n"+Url+"\n"+User+"\n"+Password);  
    System.out.println();  
}  



public static void main(String[] args) {

// loadProperties1();
// loadProperties2();
// loadProperties3();
// loadProperties4();
loadProperties5();
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值