Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

utf-8

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.forName("UTF-8")));
   }
  catch(IOException iox) {}
  System.out.println(p.getProperty("name")); //outputs null
  System.out.println(p.toString()); //outputs  { name= हिन्दी }
}

Thanks

like image 227
Naveed Quadri Avatar asked Mar 20 '26 07:03

Naveed Quadri


1 Answers

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.

like image 200
Michael Borgwardt Avatar answered Mar 22 '26 20:03

Michael Borgwardt