Java读取properties配置文件常用方法

在开发中对properties文件的操作还是蛮经常的,所以总结了几种操作方法,为后面的开发可以进行参考。

1、通过java.util.ResourceBundle类来读取

这边测试用到了枚举类进行传入文件的key值,然后获取value,可以进行灵活的配置。

通过这种方式读取properties文件不需要加.properties后缀名,只需文件名即可,如果有放在某一个包下,要加包的限定名,如放在com.frame.util包下,则要路径要用com/fram/util

config.properties:

CONFIGFILE_DIR=F:\\configDir       //两个斜杠是转义用

枚举类ConfigFileEnum.java

public enum ConfigFileEnum {
  CONFIGFILE_DIR("CONFIGFILE_DIR");
	private String name = null;

	ConfigFileEnum(String name){
		this.name = name;
	}
	public String getName(){
		return this.name;
	}

}

读取配置文件类ConfigUtil.java

public class ConfigUtil {
private static ResourceBundle resourceBundle = ResourceBundle.getBundle("config", Locale.ENGLISH);
public static String getConfigKey(ConfigFileEnum configFileEnum){
	return resourceBundle.getString(configFileEnum.getName());
}
}

测试:

	@Test
	public void testProperties(){
		String key = ConfigUtil.getConfigKey(ConfigFileEnum.CONFIGFILE_DIR);
		System.out.println(key);
	}

2、通过jdk提供的java.util.Properties类

在使用properties文件之前,还需要加载属性文件,它提供了两个方法:load和loadFromXML。

load有两个方法的重载:load(InputStream inStream)、load(Reader reader),所以,可根据不同的方式来加载属性文件。

以下提供三种方法:

1、通过当前类加载器的getResourceAsStream方法获取

InputStream inStream = TestHttpClient.class.getClassLoader().getResourceAsStream("config.properties");

2、从文件获取

InputStream inStream = new FileInputStream(new File("D:\\dir\\Frame\\src\\config.properties"));  

3、通过类加载器实现,和第一种一样

InputStream inStream =  ClassLoader.getSystemResourceAsStream("config.properties");

测试:

	@Test
	public void testProperties() throws IOException{
		Properties p = new Properties();
		InputStream inStream = TestHttpClient.class.getClassLoader().getResourceAsStream("config.properties");
		p.load(inStream);
		System.out.println(p.get("CONFIGFILE_DIR"));
	}

Java读取properties配置文件常用方法

时间: 2024-08-10 02:09:38

Java读取properties配置文件常用方法的相关文章

java读取properties配置文件总结

java读取properties配置文件总结 在日常项目开发和学习中,我们不免会经常用到.propeties配置文件,例如数据库c3p0连接池的配置等.而我们经常读取配置文件的方法有以下两种: (1).使用getResourceAsStream()方法读取配置文件. (2).使用InputStream()流去读取配置文件. 注意:在使用getResourceAsStream()读取配置文件时,要特别注意配置文件的路径的写法. this.getClass.getResourceAsStream(f

java读取properties配置文件

java读取.properties配置文件 这两天做java项目,用到属性文件,到网上查资料,好半天也没有找到一个满意的方法能让我读取到.properties文件中属性值,很是郁闷,网上讲的获取属性值大概有以下方法,以下三种方法逐渐优化,以达到最好的效果以下都以date.properties文件为例,该文件放在src目录下,文件内容为 startdate=2011-02-07 totalweek=25 方法一: public class Stweek { static private Strin

java读取.properties配置文件的几种方法

读取.properties配置文件在实际的开发中使用的很多,总结了一下,有以下几种方法(仅仅是我知道的):一.通过jdk提供的java.util.Properties类.此类继承自java.util.HashTable,即实现了Map接口,所以,可使用相应的方法来操作属性文件,但不建议使用像put.putAll这两个方法,因为put方法不仅允许存入String类型的value,还可以存入Object类型的.因此java.util.Properties类提供了getProperty()和setPr

Java 读取Properties配置文件

读取Properties配置文件的方法,经常忘记,记录下来备忘一下: package utils; import java.io.IOException;import java.io.InputStream; import java.util.Properties; public class PropertyConfig { private static Properties config; static { config = new Properties(); InputStream in =

Java读取properties配置文件时,中文乱码解决方法

碰到了用java.util.Properties读取中文内容(UTF-8格式)的配置文件,发生中文乱码的现象 Properties prop=new Properties(); prop.load(Client.class.getClassLoader().getResourceAsStream("config.properties")); 由于使用这样的加载方式使用了系统默认的编码格式,不是UTF-8格式的读取模式,就会发生乱码情况. 正确解决方法 Properties prop=n

Java 读取properties 配置文件的几种方式

基于ClassLoder读取配置文件 Properties properties = new Properties(); // 使用ClassLoader加载properties配置文件生成对应的输入流 InputStream in = PropertiesMain.class.getClassLoader().getResourceAsStream("config/config.properties"); // 使用properties对象加载输入流 properties.load(

Java读取.properties配置文件

1.源码 /** * 读取配置文件[目前只测了.properties配置文件]的工具类 * Created by li on 2015/9/13. */ public class PropertiesUtils { private static PropertiesConfiguration propertiesConfiguration; static { try { //初始化实例 propertiesConfiguration = new PropertiesConfiguration("

java读取properties配置文件方法(一)

为了修改项目参数方便,需要使用properties配置文件: 首先是需要三个jar包(不同的jar包,读取配置文件的方式会有所不同,这里使用的是2.6版本的jar包) commons comfigurations 依赖包 commons-lang commons-logging 然后开始就可以编写读取文件的代码了 现在先简单说一下第一种,如有不足之处欢迎指教. package properties; import org.apache.commons.configuration.Configur

java读取properties配置文件[转]

网上文章常见的几种读取.properties文件的方式 1.使用java.util.Properties类的load()方法 示例: InputStream in = lnew BufferedInputStream(new FileInputStream(name)); Properties p = new Properties(); p.load(in); 2.使用java.util.ResourceBundle类的getBundle()方法  ResourceBundle rb = Res