Hoa*_*huu 1 java xml enums properties
我有一个Java Enum:
public enum CodeType {
BRONZE("00001BP", "BAP"),
SILVER("00002SL", "SAP"),
GOLD("00003GL", "GAP"),
MOBILE("00004MB", "TCM"),
SOCIAL("00005SM", "ASM"),
WEB_PRESENCE("00006WP", "GLO"),
EMAIL_MARKETING("00007EM", "PEM"),
CUSTOM_DIAMOND("00008CD", "PCS"),
CONSUMER_PORTAL("00009CP", "CPS");
private String code;
private String key;
CodeType(String code, String key) {
this.code = code;
this.key = key;
}
...
}
Run Code Online (Sandbox Code Playgroud)
如你所见,我有九个元素,每个元素都有两个值.我的问题是如何从属性或xml等文件中加载这些元素的值?我的意思是:
BRONZE(isLoadedFromFile, isLoadedFromFile),
...
CONSUMER_PORTAL(isLoadedFromFile, isLoadedFromFile);
Run Code Online (Sandbox Code Playgroud)
非常感谢.
小智 10
试试这样的东西......
public enum EnumTest {
BRONZE, SILVER;
public String getProperty(String keyOrCode) {
Properties prop = new Properties();
try {
prop.load(new FileInputStream("E:\\EnumMapper.properties"));
} catch (Exception e) {
e.printStackTrace();
}
return prop.getProperty(this.name() + "." + keyOrCode);
}
public String getCode() {
return getProperty("CODE");
}
public String getKey() {
return getProperty("KEY");
}
public static void main(String[] args) {
System.out.println(EnumTest.BRONZE.getCode());
System.out.println(EnumTest.BRONZE.getKey());
}
}
Run Code Online (Sandbox Code Playgroud)
EnumMapper.properties包含的位置
BRONZE.CODE=00001BP
BRONZE.KEY=BAP
SILVER.CODE=00002SL
SILVER.KEY=SAP
Run Code Online (Sandbox Code Playgroud)
只是想分享一些可能性..
| 归档时间: |
|
| 查看次数: |
6348 次 |
| 最近记录: |