获取properties文件中的配置
2016-05-03 本文已影响43人
stepyu
常用的两种获取properties文件中配置的方法
- 通过getResourceAsStream()方法获取
代码如下:
Properties prop = new Properties();
InputStream in = this.getClass().getResourceAsStream("application.properties" );
try {
prop.load(in);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
userId = prop.getProperty("userId");
userToken = prop.getProperty("userToken");
```
- 直接通过InputStream获取流
Properties prop = new Properties();
try {
in1 = new BufferedInputStream(new FileInputStream("src/main/resources/application.properties"));
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
try {
prop.load(in);
} catch (IOException e) {
e.printStackTrace();
}
userId = prop.getProperty("userId");
userToken = prop.getProperty("userToken");