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");
方法一:
不使用其他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");