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);
    }
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM