美文网首页
properties文件转map

properties文件转map

作者: QuietBlade | 来源:发表于2023-01-11 10:25 被阅读0次

找了很久都没有一个可以用的读取properties文件然后转换成Map的源码, 这里提供一个思路, 利用Properties类的load方法来加载自定义的properties


@Bean

public Propertiesi18nMessage() {

    Properties p =new Properties();

    try{

        final ClassPathResource classPathResource =new ClassPathResource("i18n/messages.properties");

        p.load(new InputStreamReader(classPathResource.getInputStream(), "UTF-8"));

    }catch (Exception e){

        e.printStackTrace();

    }

    return p;

}

Properties 转 map就非常简单了, Map<String, String> map = new HashMap<String, String>((Map) p); 就行

相关文章

网友评论

      本文标题:properties文件转map

      本文链接:https://www.haomeiwen.com/subject/hyubcdtx.html