一、配置文件config.properties是放在src根目录下的:例如我的是 ,下面我们就来聊聊关于java读取工程外配置文件?接下来我们就一起去了解一下吧!

java读取工程外配置文件(Java读取配置文件config.properties)

java读取工程外配置文件

一、配置文件config.properties是放在src根目录下的:例如我的是

/PropertiesTest/src/com/xuliugen/project/type.properties

配置文件中的内容如下:

left=com.sunny.project.LeftHair

right=com.sunny.project.RightHair

in=com.sunny.project.InHair

读取配置文件中的代码如下:

public class PropertiesReader {

public static void main(String[] args) {

new PropertiesReader().getProperties();

}

public Map<String, String> getProperties() {

Properties props = new Properties();

Map<String, String> map = new HashMap<String, String>();

try {

InputStream in = getClass().getResourceAsStream("type.properties");

props.load(in);

Enumeration en = props.propertyNames();

while (en.hasMoreElements()) {

String key = (String) en.nextElement();

String property = props.getProperty(key);

map.put(key, property);

,