無(wú)標(biāo)題文1章

`


```

/** * 讀取資源文件工具類 * @author liyang * 2017年5月19日 */public class ResourcesUtil implements Serializable{private static final long serialVersionUID = -9217951614629363385L;/** * 系統(tǒng)語(yǔ)言環(huán)境,默認(rèn)為中文zh */public static final String LANGUAGE = "zh";/** * 系統(tǒng)國(guó)家環(huán)境,默認(rèn)為中國(guó)CN */public static final String COUNTRY = "CN";private static Locale getLocale() {Locale locale = new Locale(LANGUAGE, COUNTRY);return locale;} /** * 根據(jù)語(yǔ)言、國(guó)家、資源文件名和key名字獲取資源文件值 * @param baseName 資源文件名 * @param language 語(yǔ)言 * @param country? 國(guó)家 * @param section? key名字 * @return 值 */private static String getProperties(String baseName, String section) {String retValue = "";try {Locale locale = getLocale();ResourceBundle rb = ResourceBundle.getBundle(baseName, locale);retValue = (String) rb.getObject(section);} catch (Exception e) {e.printStackTrace();}return retValue;}/** * 通過(guò)key從資源文件讀取內(nèi)容 * @param fileName 資源文件名 * @param key 索引 * @return 索引對(duì)應(yīng)的內(nèi)容 */public static String getValue(String fileName,String key) {return getProperties(fileName, key);}/** * 獲取資源文件下所有的索引名 * @param baseName 資源文件名 * @return */public static ListgetKeyList(String baseName) {Locale locale = getLocale();ResourceBundle rb = ResourceBundle.getBundle(baseName,locale);Listreslist = new ArrayList();Setkeyset = rb.keySet();for (Iteratorit = keyset.iterator(); it.hasNext();) {

String lkey = it.next();

reslist.add(lkey);

}

return reslist;

}

/**

* 通過(guò)key從資源文件讀取內(nèi)容,并格式化

*

* @param fileName

*? ? ? ? ? ? 資源文件名

*

* @param key

*? ? ? ? ? ? 索引

*

* @param objs

*? ? ? ? ? ? 格式化參數(shù)

*

* @return 格式化后的內(nèi)容

*/

public static String getValue(String fileName, String key, Object[] objs) {

String pattern = getValue(fileName, key);

String value = MessageFormat.format(pattern, objs);

return value;

}

public static void main(String[] args) {

System.out.println(getValue("resources.messages", "105", null));

//信息格式化,如果資源中有{}的參數(shù)則需要使用MessageFormat格式化,Object[]為傳遞的參數(shù),數(shù)量根據(jù)資源文件中的{}個(gè)數(shù)決定

// System.out.println(getValue("resources.messages", "102", new Object[]{100,200}));

}

}

```

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容