读取properties中文乱码解决

方法1:对应解码

result=new String(result.getBytes("ISO-8859-1"), "utf-8");
但是不同的机器读取properties的编码方式可能不同,这种不推荐

方法2:用字符流

public static Properties loadResource(String propFile) throws IOException {
        Properties properties = new Properties();
        InputStreamReader reader = new InputStreamReader(getClass().getResourceAsStream(fileClassPath));
        properties.load(reader);
        return properties;
    }

public void get(){
        Properties prop = null;
        try {
            prop = loadResource("application.properties");
        } catch (IOException e) {
            e.printStackTrace();
        }
        String title = null;
        try {
            title = new String(prop.getOrDefault("name", "小明").toString().getBytes(),"UTF-8");
        } catch (UnsupportedEncodingException e) {
        }
     }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容