ResourceBundle bundle= ResourceBundle.getBundle( "bundle ");

import java.util.*;
//
//如果 ListResourceBundle 或 PropertyResourceBundle 不符合您的需求,
//您可写自己的 ResourceBundle 子类。
//您写的子类必须覆盖两个方法:handleGetObject 和 getKeys()。
class MyResource extends ListResourceBundle
{
    public Object[][] getContents()
    {
        return contents;
    }

    static final Object[][] contents = {
        // LOCALIZE THIS
        {"OkKey", "OK"},
        {"CancelKey", "Cancel"},
        // END OF MATERIAL TO LOCALIZE
    };

}

 

import java.util.*;
class  MyResourceBundle
{
    public static void main(String[] args) 
    {
        ResourceBundle myResourceBundle=new MyResource();
        String button1 = myResourceBundle.getString("OkKey");
        String button2 = myResourceBundle.getString("CancelKey");
        System.out.println(button1 + "!" + button2);
    }
}

 

 

ResourceBundle最大的好处是可以使你的程序国际化,在bundle文件名后加上国家、语言关键字,那系统就会自动载入相应的bundle文件。

比如:

bundle.properties 缺省

bundle_zh.properties 中文

bundle_ja.properties 日文

ResourceBundle bundle= ResourceBundle.getBundle( "bundle ");

如果你的系统是中文的,那就会自动加载bundle_zh.properties

需要注意的是,多字节编码的bundle需要用native2ascii转成unicode编码。

package com.skex.test;

import java.util.Arrays;
import java.util.List;
import java.util.ResourceBundle;

public class Test {

    protected ResourceBundle res = ResourceBundle.getBundle("SetupResources");
    
    public List getAll(){
        String[] SupportList = res.getString("SupportList").split(",");
        if(SupportList!=null){
            List all=Arrays.asList(SupportList);
            return all;
        }
        return null;
    }
    
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Test t=new Test();
        List l=t.getAll();
        if(l!=null){
            String[] a=(String[]) l.toArray();
            for(String s:a){
                System.out.println(s);
            }            
        }        
    }

}

//####### D:\XXX\src\SetupResources.properties
//SupportList=Jim,Tom,Lucy
//
//
/*
Jim
Tom
Lucy
*/

 

posted @ 2017-09-22 10:53  sky20080101  阅读(113)  评论(0)    收藏  举报