本文实例讲述了Java实现的读取资源文件工具类ResourcesUtil。分享给大家供大家参考,具体如下:
package com.gcloud.common;
import java.io.Serializable;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.Set;
public class ResourcesUtil implements Serializable {
private static final String FILENAME = "conf.messages";
private static final long serialVersionUID = -7657898714983901418L;
public static final String LANGUAGE = "zh";
public static final String COUNTRY = "CN";
private static Locale getLocale() {
Locale locale = new Locale(LANGUAGE, COUNTRY);
return locale;
}
private static String getProperties(String baseName, String section) {
try {
ResourceBundle rb = ResourceBundle.getBundle(baseName, getLocale());
return (String) rb.getObject(section);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String getValue(String fileName, String key) {
return getProperties(fileName, key);
}
public static String getValue(String key) {
return getProperties(FILENAME, key);
}
public static List getKeyList(String baseName) {
ResourceBundle rb = ResourceBundle.getBundle(baseName, getLocale());
List reslist = new ArrayList();
Set keyset = rb.keySet();
for (Iterator it = keyset.iterator(); it.hasNext();) {
String lkey = (String) it.next();
reslist.add(lkey);
}
return reslist;
}
public static String getValue(String fileName, String key, Object[] objs) {
String pattern = getValue(fileName, key);
return MessageFormat.format(pattern, objs);
}
public static void main(String[] args) {
//908=操作成功{0}条,失败{1}条,点击查看失败信息
System.out.println(getValue("conf.messages", "908", new Object[] { 100, 200 }));
}
}
更多关于java算法相关内容感兴趣的读者可查看本站专题:《Java文件与目录操作技巧汇总》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》和《Java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。



