使用InputStream读取Properties文件,下面详情说明:
1. config.properties文件,放在资源目录下,例如resource/config.properties
文件内容为:
wxtoken=123456
wxappid=wxe234detre7c0
2.使用 InputStream解析config.properties文件的代码如下:
public class PropertiesUtil {
public static void main(String[] args) {
// TODO Auto-generated method stub
InputStream fis = PropertiesUtil.class.getClassLoader()
.getResourceAsStream("config.properties");
try {
Properties mConfig = new Properties();
mConfig.load(new InputStreamReader(fis, "utf-8"));
System.out.println(mConfig.getProperty("wxtoken"));
System.out.println(mConfig.getProperty("wxappid"));
} catch (Exception e) {
e.printStackTrace();
}
}}
3.运行后在控制台输出如下信息:
123456
wxe234detre7c0