web应用,界面的频繁操作可能出现的错误情况需要准确描述出原因
经常使用properties文件来整理错误码与错误描述的对应关系
那么在Java web应用中该如何简单的读取properties文件并且适时加载properties文件呢?
今天尝试了一下,网络上关于Java读取properties文件的方法用的比较多的有两种:
一种是采用bundle
public class ReadProperties
{
public static Map<String,String> resourceMap = new HashMap<String,String>();
public static void initRegistResource()
{
ResourceBundle bundle = null;
//不论这个properties文件在哪个包下,只需要标明所在包名加properties文件的名字(不加后缀)就可以了
bundle = ResourceBundle.getBundle("config.errorDescription",Locale.English);
for(String key : bundle.keySet())
{
if(resourceMap.get(key) == null)
{
resourceMap.put(key,bundle.getString(key);
}
}
}
}
在加载Servlet的时候,初始化注册properties文件的资源,后续就可以直接获取错误码描述了
这种方法很简单,但是可能会引起Applet比较慢的问题,具体原因还不清楚