java读取properties文件的几种方法

一、项目中经常会需要读取配置文件(properties文件),因此读取方法总结如下: 

1、通过java.util.Properties读取 

Properties p=new Properties();
//p需要InputStream对象进行读取文件,而获取InputStream有多种方法:
//1、通过绝对路径:InputStream is=new FileInputStream(filePath);
//2、通过Class.getResourceAsStream(path);
//3、通过ClassLoader.getResourceAsStream(path); 
p.load(InputStream is);
is.close();
p.getString(String(key))

2、通过java.util.ResourceBundle读取 

ResourceBundle rb=ResourceBundle.getBundle(packageName);
rb.getString(String key);


更新properties

private static Properties props = new Properties();  
public static void writeProperties(String keyname,String keyvalue) {          
        try {   
            // 调用 Hashtable 的方法 put,使用 getProperty 方法提供并行性。   
            // 强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。   
            OutputStream fos = new FileOutputStream("qrcode.properties");   
            props.setProperty(keyname, keyvalue);   
            // 以适合使用 load 方法加载到 Properties 表中的格式,   
            // 将此 Properties 表中的属性列表(键和元素对)写入输出流   
            props.store(fos, "Update '" + keyname + "' value");   
        } catch (IOException e) {   
            System.err.println("属性文件更新错误");   
        }   
    }   

读取

public static String readValue(String key) {   
        Properties props = new Properties();   
        try {   
            InputStream in = new BufferedInputStream(new FileInputStream(   
                    "qrcode.properties"));   
            props.load(in);   
            String value = props.getProperty(key);   
            System.out.println(key +"键的值是:"+ value);   
            return value;   
        } catch (Exception e) {   
            e.printStackTrace();   
            return null;   
        }   
    }  

二、Class.getResourceAsStream与ClassLoader.getResourceAsStream的区别 
首先,Java中的getResourceAsStream有以下几种: 
1. Class.getResourceAsStream(String path) : path 不以’/'开头时默认是从此类所在的包下取资源,以’/'开头则是从ClassPath根下获取。其只是通过path构造一个绝对路径,最终还是由 ClassLoader获取资源。 

2. Class.getClassLoader.getResourceAsStream(String path) :默认则是从ClassPath根下获取,path不能以’/'开头,最终是由ClassLoader获取资源。 

3. ServletContext. getResourceAsStream(String path):默认从WebAPP根目录下取资源,Tomcat下path是否以’/'开头无所谓,当然这和具体的容器实现有关。 

4. Jsp下的application内置对象就是上面的ServletContext的一种实现。 

其次,getResourceAsStream 用法大致有以下几种: 

第一: 要加载的文件和.class文件在同一目录下,例如:com.x.y 下有类me.class ,同时有资源文件myfile.xml 

那么,应该有如下代码: 

me.class.getResourceAsStream("myfile.xml"); 

第二:在me.class目录的子目录下,例如:com.x.y 下有类me.class ,同时在 com.x.y.file 目录下有资源文件myfile.xml 

那么,应该有如下代码: 

me.class.getResourceAsStream("file/myfile.xml"); 

第三:不在me.class目录下,也不在子目录下,例如:com.x.y 下有类me.class ,同时在 com.x.file 目录下有资源文件myfile.xml 

那么,应该有如下代码: 

me.class.getResourceAsStream("/com/x/file/myfile.xml"); 

总结一下,可能只是两种写法 

第一:前面有 “   / ” 

“ / ”代表了工程的根目录,例如工程名叫做myproject,“ / ”代表了myproject 

me.class.getResourceAsStream("/com/x/file/myfile.xml"); 

第二:前面没有 “   / ” 

代表当前类的目录 

me.class.getResourceAsStream("myfile.xml"); 

me.class.getResourceAsStream("file/myfile.xml"); 

最后,自己的理解: 
getResourceAsStream读取的文件路径只局限与工程的源文件夹中,包括在工程src根目录下,以及类包里面任何位置,但是如果配置文件路径是在除了源文件夹之外的其他文件夹中时,该方法是用不了的。 


错误: 
class.getClassLoader().getResource("*********");这一句抛出空指针异常java.lang.NullPointerException,定位为getClassLoader()返回null 

如果一个类是通过bootstrap 载入的,那我们通过这个类去获得classloader的话,有些jdk的实现是会返回一个null的, 
解决: 
修改代码如下: 


InputStream inputStream;      
ClassLoader cl = XXX. class .getClassLoader();      
if  (cl !=  null ) {      
         inputStream = cl.getResourceAsStream( "xx.properties" );      
}  else {      
         inputStream = ClassLoader.getSystemResourceAsStream( "xx.properties" );      
}      
Properties dbProps =  new  Properties();      
dbProps.load(inputStream);      
inputStream.close(); 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值