java读取properties中文乱码

1 确认properties文件的编码是utf-8

2 采用流的方式读取文件,设置编码为utf-8

public class ErrorCodeConfig {

    static Properties props;
    
    static {
       InputStreamReader isr = null;
       try {
            isr = new InputStreamReader(ErrorCodeConfig.class.getClassLoader().getResourceAsStream("config/XXXErrCode.properties"),"utf-8");
            props = new Properties();
            props.load(isr);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static String getErrorCodeDesc(String errCode) {
            String errorDescString = props.getProperty(errCode, "");
          return  errorDescString;
    }
    
    public static void main(String[] args) {
        String desc = getErrorCodeDesc("403");
        System.out.println(desc);
    }
}

 

转载于:https://www.cnblogs.com/moris5013/p/10518392.html

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