读取classess目录下的文件
代码实现
/**
* 根据资源名称获取Properties对象
* @param propertiesName
* @return
*/
protected static Properties getProperties(String propertiesName) {
String path = SystemPublicResourcesController.class.getProtectionDomain().getCodeSource().getLocation().getPath() + propertiesName;
File file = new File(path);
Properties prop = new Properties();
InputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(file));
prop.load(in);
} catch (IOException e) {
logger.error(e.getMessage(), e);
}finally {
if(in != null) {
try {
in.close();
} catch (IOException ex) {
logger.error(ex.getMessage(), ex);
}
}
}
return prop;
}
代码解读
- propertyName指的是文件名,例如:system.properties
- 实现文件读取的方式有至少5种 ,这里就不详细列举了。
- https://www.cnblogs.com/hafiz/p/5876243.html
学习Java的同学注意了!!!
学习过程中遇到什么问题或者想获取学习资源的话,欢迎加入Java学习交流群,群号码:543120397 我们一起学Java!