两种方法如下:
package com.base.code.test; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class getProperty { /** * 方法一:通过java.util.ResourceBundle读取 * @param path * 例:system.properties只需写system * @param name * 例:mmsUrl * @return */ public static String getPropertyByName(String path, String name){ String result = ""; result = java.util.ResourceBundle.getBundle(path).getString(name); return result; } /** * 方法二:通过类加载目录getClassLoader()加载 * @param path * 例:system.properties * @param name * 例:mmsUrl * @return */ public static String getPropertyByName2(String path, String name){ String result = ""; //getProperty为类名 InputStream in = getProperty.class.getClassLoader().getResourceAsStream(path); Properties prop = new Properties(); try { prop.load(in); result = prop.getProperty(name).trim(); System.out.println("name:" + result); } catch (IOException e) { System.out.println("读取配置文件出错"); e.printStackTrace(); } return result; } }
参考地址: http://blog.csdn.net/xumengxing/article/details/9153183