读取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) {
        }
     }

你可能感兴趣的:(读取properties中文乱码解决)