Java读取.properties配置文件的步骤

Java读取.properties配置文件的方法

  在Java项目中,很多信息我们都可以放在配置文件中,增加程序的灵活性,如果是修改那个参数,只需要修改配置文件的信息就可以,而不需要去修改程序里面的参数代码。

Java有个Properties的类,类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串,通过Properties里面的load方法加载输入流读取的配置文件属性列表(键和元素对)。

 

package com.lzb.common;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
/**
 * 
 * 功能描述:读取property文件
 *
 * @author  lizhenbin
 *
 * <p>修改历史:(修改人,修改时间,修改原因/内容)</p>
 */
public class PropFileReader {
	
	private static PropFileReader instance = null;
	public PropFileReader() {};
	public static synchronized PropFileReader getInstance() {
		if(instance == null)
			instance = new PropFileReader();
		return instance;
	}
	
	/**
	 * 
	 * 功能描述:读取配置文件
	 *
	 * @author  lizhenbin
	 * <p>创建日期 :2012-2-7 下午5:29:02</p>
	 *
	 * @param path 文件的完整路径 com/lzb/pro/xxx.properties
	 * @return 配置文件的内容,存放在HashMap中;异常情况,返回null
	 *
	 * <p>修改历史 :(修改人,修改时间,修改原因/内容)</p>
	 */
	public static Map<Object, Object> read(String path) {
		
		Map<Object, Object> map = new HashMap<Object, Object>();
		StringBuffer sb = new StringBuffer();
		/**
		 * 获取字节码的路径
		 */
		URL url = PropFileReader.class.getResource("/");
		sb.append(url.getPath() + path);
		String absolutePath = sb.toString();
		File file = new File(absolutePath);
		
		InputStreamReader ins = null;
		BufferedReader reader = null;
		try {
			// 解决读取配置文件的中文的乱码问题
			ins = new InputStreamReader(new FileInputStream(file),"UTF-8");
			reader = new BufferedReader(ins);			
			Properties prop = new Properties();
			//读取udp.properties
			prop.load(reader);			
			Enumeration enums = prop.propertyNames();
			while(enums.hasMoreElements()) {
				String key = (String) enums.nextElement();
				String value = prop.getProperty(key);
				map.put(key, value);
			}
			return map;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			try {
				ins.close();
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
				return null;
			}
		}		
	}
}

 

 补充兼容JDK1.5版本的读取配置文件类

package com.commsoft.ypass.smp.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
/**
 * 
 * 功能描述:读取property文件
 *
 * @author  lizhenbin
 *
 * <p>修改历史:(修改人,修改时间,修改原因/内容)</p>
 */
public class PropertyRead {
	
	/**
	 * 单例模式
	 */
	private static PropertyRead instance = null;
	public PropertyRead() {};
	public static synchronized PropertyRead getInstance() {
		if(instance == null)
			instance = new PropertyRead();
		return instance;
	}
	
	/**
	 * 
	 * 功能描述:读取配置文件,兼容JDK1.5
	 *
	 * @author  lizhenbin
	 * <p>创建日期 :2012-2-7 下午5:29:02</p>
	 *
	 * @param path 文件的完整路径 com/lzb/pro/xxx.properties
	 * @param encode 读文件字符编码
	 * @return 配置文件的内容,存放在HashMap中;异常情况,返回null
	 *
	 * <p>修改历史 :(修改人,修改时间,修改原因/内容)</p>
	 */
	public static Map<Object, Object> read(String path, String encode) {
		
		Map<Object, Object> map = new HashMap<Object, Object>();
		StringBuffer sb = new StringBuffer();
		/**
		 * 获取字节码的路径
		 */
		URL url = PropertyRead.class.getResource("/");
		sb.append(url.getPath() + path);
		String absolutePath = sb.toString();
		File file = new File(absolutePath);
		
		FileInputStream ins = null;
		try {
			// 解决读取配置文件的中文的乱码问题
			ins = new FileInputStream(file);		
			Properties prop = new Properties();
			//读取udp.properties
			prop.load(ins);			
			Enumeration<?> enums = prop.propertyNames();
			while(enums.hasMoreElements()) {
				String key = (String) enums.nextElement();
				String value = new String(prop.getProperty(key).getBytes("ISO-8859-1"), encode);;
				map.put(key, value);
			}
			return map;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			if(null != ins)
				try {
					ins.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}		
	}
}