java property file as enum

前端 未结 8 1077
梦如初夏
梦如初夏 2020-12-24 09:47

Is it possible to convert a property file into an enum.

I have a propoerty file with lot of settings. for example

equipment.height
equipment.widht
e         


        
8条回答
  •  自闭症患者
    2020-12-24 10:13

    public enum ErrorCode{
    
    
        DB_ERROR( PropertiesUtil.getProperty("DB_ERRROR_CODE"), PropertiesUtil.getProperty("DB_ERROR")),
        APP_ERROR(PropertiesUtil.getProperty("APPLICATION_ERROR_CODE"), PropertiesUtil.getProperty("APPLICATION_ERROR")),
        ERROR_FOUND(PropertiesUtil.getProperty("ERROR_FOUND_CODE"), PropertiesUtil.getProperty("ERROR_FOUND"));
    
    
        private final String errorCode;
        private final String errorDesc;
    
    
    
        private ErrorCode(String errorCode, String errorDesc) {
            this.errorCode = errorCode;
            this.errorDesc = errorDesc;
        }
    
        public String getErrorDesc() {
            return errorDesc;
        }
    
        public String getErrorCode() {
            return errorCode;
        }
    
        public static String getError(String errorCode)
        { 
            System.out.println("errorCode in Enum"+errorCode);
            System.out.println(java.util.Arrays.asList(ErrorCode.values()));
            for (ErrorCode errorEnum : ErrorCode.values()) {
                System.out.println(errorEnum.errorCode);
                System.out.println(errorEnum.errorDesc);
            if ((errorEnum.errorCode).equals(errorCode)) {
                return errorEnum.getErrorDesc();
            }
            }
            return ERROR_FOUND.getErrorDesc();
    
        }
    
    
    public class PropertiesUtil {
    static  Properties prop = new Properties();
    
        static{
    
            try {
    
                  InputStream inputStream =
                          PropertiesUtil.class.getClassLoader().getResourceAsStream("db.properties");
    
                 prop.load(inputStream);
        }catch(Exception e)
            {
            e.printStackTrace();
            }
        }
    
    
    
            public static PropertiesUtil getInstance()
            {
    
                return new PropertiesUtil();
            }
    
            public Properties getProperties()
            {
                return prop;
            }
    
            public static String getProperty(String key)
            {
                return prop.getProperty(key);
            }
    
    
    }
    

提交回复
热议问题