`
conquer0
  • 浏览: 76190 次
  • 性别: Icon_minigender_1
  • 来自: 广州
文章分类
社区版块
存档分类
最新评论

java读取properties文件

阅读更多
java读取properties文件
方法一:
不使用其他jar包,使用jdk自带的方法:java.lang.ClassLoader下的getResourceAsStream(String fileName)即可:
package com.huawei.test;

import java.io.InputStream;
import java.util.Properties;

public class JavaTest
{
    public static void main(String[] args) throws Exception
    {
        InputStream in = new JavaTest().getClass().getClassLoader().getResourceAsStream("com/huawei/file/config.properties");
        Properties properties = new Properties();
        properties.load(in);
//假设config.properties有两个属性 ---> name:wangweidong;sex:male
        System.out.println("姓名:"+properties.getProperty("name")+" 性别: "+properties.getProperty("sex"));
    }
}

方法二:
下载apache中commons project中的configuration jar包,我当时下的是:commons-configuration-1.6.jar,还得需要commons-collections-3.2.1.jar,commons-lang-2.5.jar,commons-logging.jar
Configuration config = new PropertiesConfiguration("com/huawei/file/config.properties");
config.getString("name");
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics