且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

解决JAVA Properties类读取配置文件中文乱码的问题

更新时间:2021-12-23 02:34:02

使用Propertis类读取配置文件的方法通常如下:

InputStream inputStream = PropertiesBean.class.getClassLoader().getResourceAsStream("config.properties");
properties.load(inputStream);

但是如果配置文件中如果包含中文,就会出现乱码,所以可以通过中转的形式优化一下:

InputStream inputStream = PropertiesBean.class.getClassLoader().getResourceAsStream("config.properties");
properties.load(new InputStreamReader(inputStream,"UTF-8"));

通过该方式即可解决中文问题。

解决JAVA Properties类读取配置文件中文乱码的问题

如果有更好的处理方式,可以评论留言。