package com.tech01.common.utils; import java.text.MessageFormat; import java.util.HashMap; import java.util.Map; import java.util.ResourceBundle;/** * 资源文件工具类 * */ public class ResourceUtils {private ResourceBundle resourceBundle; private ResourceUtils(String resource) { resourceBundle = ResourceBundle.getBundle(resource); } /** * 获取资源 * resource 资源 * 解析 */ public static ResourceBundle getResource(String resource) { return ResourceBundle.getBundle(resource); } /** * 根据key取得value * key 键值 * args value中参数序列,参数:{0},{1}...,{n} * */ public String getValue(String key, Object... args) { String temp = resourceBundle.getString(key); return MessageFormat.format(temp, args); } /** * 获取所有资源的Map表示 * @return 资源Map */ public Map<String, String> getMap() { Map<String, String> map = new HashMap<String, String>(); for(String key: resourceBundle.keySet()) { map.put(key, resourceBundle.getString(key)); } return map; } } |