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);

System.out.println(key + " " + property);

}

} catch (Exception e) {

e.printStackTrace();

}

return map;

}

}

运行结果如下:

Java读取配置文件config.properties

推荐阅读