这篇文章将为大家详细讲解有关Java怎么实现的读取资源文件工具类ResourcesUtil实例,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
本文实例讲述了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<String> getKeyList(String baseName) { ResourceBundle rb = ResourceBundle.getBundle(baseName, getLocale()); List<String> reslist = new ArrayList<String>(); Set<String> keyset = rb.keySet(); for (Iterator<String> 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}条,<a href=\"{2}\" target=\"_blank\">点击查看失败信息</a> System.out.println(getValue("conf.messages", "908", new Object[] { 100, 200 })); }
关于“Java怎么实现的读取资源文件工具类ResourcesUtil实例”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。