美文网首页
无标题文1章

无标题文1章

作者: 陌路的mvp | 来源:发表于2017-05-19 15:19 被阅读0次

`

```

/** * 读取资源文件工具类 * @author liyang * 2017年5月19日 */public class ResourcesUtil implements Serializable{private static final long serialVersionUID = -9217951614629363385L;/** * 系统语言环境,默认为中文zh */public static final String LANGUAGE = "zh";/** * 系统国家环境,默认为中国CN */public static final String COUNTRY = "CN";private static Locale getLocale() {Locale locale = new Locale(LANGUAGE, COUNTRY);return locale;} /** * 根据语言、国家、资源文件名和key名字获取资源文件值 * @param baseName 资源文件名 * @param language 语言 * @param country  国家 * @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;}/** * 通过key从资源文件读取内容 * @param fileName 资源文件名 * @param key 索引 * @return 索引对应的内容 */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;

}

/**

* 通过key从资源文件读取内容,并格式化

*

* @param fileName

*            资源文件名

*

* @param key

*            索引

*

* @param objs

*            格式化参数

*

* @return 格式化后的内容

*/

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));

//信息格式化,如果资源中有{}的参数则需要使用MessageFormat格式化,Object[]为传递的参数,数量根据资源文件中的{}个数决定

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

}

}

```

相关文章

  • 无标题文gs章

    无标题文gs章无标题文gs章无标题文gs章

  • 无标题文是的撒章

    无标题文是的撒章无标题文是的撒章无标题文是的撒章无标题文是的撒章

  • 无标题文652fg2d1gd章

    无标题文652fg2d1gd章无标题文652fg2d1gd章无标题文652fg2d1gd章

  • 无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章 无标题文章无标题文章无标题文章无...

  • 无标题文章

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章

  • 无标题文章

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标...

  • 无标题文章

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标...

  • fasfsdfdf

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标...

  • 无标题文章

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标...

  • 无标题文章

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标...

网友评论

      本文标题:无标题文1章

      本文链接:https://www.haomeiwen.com/subject/oxpyxxtx.html