java.util.ResourceBundle学习笔记

一、初次使用,从网上查的资料,知识点参考JDK API和博文http://lavasoft.blog.51cto.com/62575/184605(该博主写的清晰易懂)

二、自己在程序中的具体应用:

①src下建立文件test.properties,其中内容:name=google (项目在不同地区该配置文件中的name值不同)

  1. ②java程序中:
    /**
     * 根据配置文件中的值,设置url路径
     * @param propertiesFileName 配置文件的文件名
     * @param itemName 读取的配置文件中的key的值
     * @return url路径
     */
    public static String getPath(String propertiesFileName, String itemName){
        String path = "";
        try{
            ResourceBundle resources = ResourceBundle.getBundle(propertiesFileName);
            String itemValue = resources.getString(itemName);
            if(("google").equals(itemValue.substring(0, 4))){
            	path="www.google.com.hk";
    		}else if(("baidu").equals(itemValue.substring(0, 4))){
    			path="www.baidu.com";
    		}else{
    			path="www.bing.com";
    		}
        }
        catch(Exception e){
        	path="www.bing.com";
        	e.printStackTrace();
        }
        return path;
    }

③调用的时候,getPath("test.properties","name");返回:"www.google.com.hk"

这个用法跟“一”中的“特定于语言环境”无关,只是在不同地区该项目配置文件中的值不同,该方法只是用来取得这个properties文件中的值,再在程序中后续操作而已。

待续……

原文地址:https://www.cnblogs.com/mySummer/p/4073742.html