1.Springframeworks中的ClassPathResource类
用途:用来指定需要classLoader加载的资源(.properties/.yml)
ClassPathResource resource = new ClassPathResource(
资源的位置
)
2.Springframeworks里的PropertiesLoaderUtils 类
用途:用来获取资源文件的内容
Properties
properties = PropertiesLoaderUtils.loadProperties(
ClassPathResource的实体类
)
说明:
Properties类
实现了Map接口,因此Properties类本质上是一种简单的Map容器
Properties类与properties关系:
Properties类本身表示了对一种Map结构的操作,properties文件本身就表示了一个“键值对”的集合
(1)Properties类可以通过绝对/相对路径方式加载properties文件(读取properties文件)
-->在spring框架中使用
ClassPathResource + PropertiesLoaderUtils
-->普通情况下使用Properties类中的load()方法 =>
Properties类对象.load(InputStream inStream)
(2)将程序中map集合通过Properties类保存为properties文件(保存为properties文件)
-->使用Properties类中的store()方法 =>
properties.store(OutputStream out, String comments)