无法从java中的.properties资源文件中读取泰文字符

时间:2014-01-03 09:29:31

标签: java properties resourcebundle thai

我有一个有泰国字符的资源文件(.properties文件)。

当我使用下面的代码阅读该文件时,它显示的垃圾字符如“?”

package RD1.Common;

import java.util.Enumeration;
import java.util.Locale;
import java.util.ResourceBundle;

public class LabelManagerRD {

   public static String[] getLabel(String ParamString1)
    {

      String NextEle = "";
      String str2 = ParamString1;
      int i = 1;

      String Final[] = new String[1000];

      ResourceBundle bundle =
              ResourceBundle.getBundle("rd", Locale.US);

      Enumeration<String> enumeration = bundle.getKeys();

      while (enumeration.hasMoreElements()) 
        {
             NextEle = enumeration.nextElement();

             if (NextEle.toLowerCase().contains(str2.toLowerCase()))
             {
                 Final[i] = NextEle+"="+bundle.getString(NextEle);
                 i++;
             }
      }

    return Final;

    }


   public static void main(String[] args)
    {
       try
       {
        String TestValue[] = getLabel("RD.RDRAPCEX");
        for(int i=1;i<=TestValue.length;i++)
        {   
            if (!(TestValue[i].length()==0))
            {
                System.out.println(i+" - "+TestValue[i]);
            }

        }
       }
       catch (Exception e)
       {

       }


    }
}

属性文件(rd_en_US.properties)如下所示

BL_BLNG_GROUP.BL_BLNG_GRP.BLNG_GRP_ID.IP=รสสรืเ เพนีย รก~^PAGE_1~^Y~^N
BL_BLNG_GROUP.BL_BLNG_GRP.LONG_DESC.IP=Long Desc~^PAGE_1~^Y~^N
BL_BLNG_GROUP.BL_BLNG_GRP.SHORT_DESC.IP=Short Desc~^PAGE_1~^Y~^N
BL_BLNG_GROUP.BL_BLNG_GRP.DETAIL_DESC.IP=Explanatory Note~^PAGE_1~^Y~^N

请建议如何处理此事。

提前致谢, 沙

2 个答案:

答案 0 :(得分:1)

如果您的文件编码是正确的,那么您必须注意System.out将无法使用默认控制台设置打印UTF-8字符。确保用于显示输出的控制台也以UTF-8编码。

例如,在Eclipse中,您需要转到“运行配置”&gt;通常这样做。

enter image description here

答案 1 :(得分:1)

属性文件通常以ISO 8859-1编码进行解释。如果您需要此集合中未包含的其他字符,请使用\uxxxx之类的unicode转义符。还有一些工具可用于将具有不同编码的属性文件转换为此文件(请参阅native2ascii)。