开发者

java.util.properties getProperty method returns null for UTF-8 string

开发者 https://www.devze.com 2023-04-10 05:10 出处:网络
I have a problem getting localized value (a UTF-8 string) from the properties file. Here is what i did ..

I have a problem getting localized value (a UTF-8 string) from the properties file.

Here is what i did ..

//properties file containing
//name=हिन्दी
public static void main(String[] args)
{
  File f = new File("path\\to\\properties\\file");
  Properties p = new Properties();
  try
   {
     p.load(new InputStreamReader(new FileInputStream(f),Charset.f开发者_JAVA百科orName("UTF-8")));
   }
  catch(IOException iox) {}
  System.out.println(p.getProperty("name")); //outputs null
  System.out.println(p.toString()); //outputs  { name= हिन्दी }
}

Thanks


If the last line actually outputs name= हिन्दी then it looks like there's something wrong with the key String in the properties file; perhaps in includes some invisible whitespace character or one of the charaters of "name" is not the regular latin letter but something that looks like it. Take a look at the properties file in a hex editor.


Your problem is here:

catch(IOException iox) {}

this is called "mutual failure". Never do this. Your code that loads properties from file fails and throws exception and you even do not know about this. Add e.printStackTrace() and move code that prints properties values into the try/catch block. See the exception and fix the bug. I believe that your file path is wrong, so you will get FileNotFoundException or probably you do not have access rights to this file.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号