read resourcebundle as UTF-8. getString() Method seems to change encoding to ISO-8859
Java ResourceBundles assume ISO-8859. I think you'll need to use Properties instead of ResourceBundle.
InputStream utf8in = getClass().getClassLoader().getResourceAsStream("/path/to/utf8.properties");
Reader reader = new InputStreamReader(utf8in, "UTF-8");
Properties props = new Properties();
props.load(reader);
UTF-8 encoding issues with Spring resourcebundle for special characters
I was having the same issue and @sunny_dev's answer worked for me.
I don't understand why there is not answer for this question yet so I'm updating the question.
@sunny_dev answer:
Karol, I solved the problem by opening and saving the .properties file
in TextPad as "UTF-8" encoding and "Unix" platform. I had taken that
same approach, however in Notepad++ without the positive outcome
earlier. – sunny_dev
Thanks again to @sunny_dev
How to set encoding for bundle?
Problem: By default ISO 8859-1 character encoding is used for reading content of properties file, so if the file contains any character beyond ISO 8859-1 then it will not be processed properly.
First solution:
Process the properties file to have content with ISO 8859-1 character encoding by using native2ascii - Native-to-ASCII Converter
What native2ascii does: It converts all the non-ISO 8859-1 character in their equivalent \uXXXX. This is a good tool because you need not to search the \uXXXX equivalent of special character.
Usage for UTF-8: native2ascii -encoding utf8 e:\a.txt e:\b.txt
If you use Eclipse then you will notice that it implicitly converts the special character into \uXXXX equivalent. Try copying
会意字 / 會意字
into a properties file opened in Eclipse.
Second solution: Create a custom ResourceBundle.Control
class which can be used with ResourceBundle
to read properties in any given encoding scheme. You may want to use an already created and shared CustomResourceBundleControl.
Use it as below:
ResourceBundle messages = ResourceBundle.getBundle("i18n/Messages", new CustomResourceBundleControl("UTF-8"));
Reading properties file via Resourcebundle encoding issue
I believe you are correct in assuming the resourcebundle is read in as iso-8859-1.
Javadoc of Properties class
(source)
Are you sure your properties file is saved under the iso-8859-1 format?
I believe Notepad++ provides the functionality to at least check the encoding, if not convert it.
Java ResourceBundles does not read utf-8 characters correctly - after upgrading Eclipse
Eventually I didn't create a new workspace - I wanted my code to work under any platform, "in any condition". So I resolved it by changing the code to use Properties instead of Resources.
As input parameter I used a Reader with "utf-8" encoding set:
ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
Resource[] fileInJar = resourceResolver.getResources(filePath);
Properties properties = new Properties();
BufferedReader reader = new BufferedReader(new InputStreamReader(fileInJar[0].getInputStream(), "UTF-8"));
properties.load(reader);
Thanks for those who answered! I really appreciate it...
Carmel
Related Topics
Socket Using in a Swing Applet
Simplest Way to Read Json from a Url in Java
How to Read from a Inputstream With a Timeout
Swing Animation Running Extremely Slow
Byte Order Mark Screws Up File Reading in Java
How to Secure an API Rest For Mobile App (If Sniffing Requests Gives You the "Key")
How to Run a Java Program from the Command Line on Windows
How to Find All Subclasses of a Given Class in Java
Split String to Equal Length Substrings in Java
Java Error: Comparison Method Violates Its General Contract
Difference Between Jdk and Jre
Getting the Name of the Currently Executing Method