Read Japanese Characters from a properties file using Resource Bundle

2.6k views Asked by At

I am working on developing a project and when I attempt to read in a Japanese character string from a properties file using ResourceBundle in Java, all I get are a bunch of question marks (??????????) displayed in my application.

I know that I need to encode the string somehow, but I've tried several different ways with no luck. I've also tried to encode the properties file in a text editor but this doesn't seem to work how I'd like either. My code for retrieving the string is below. I know you can do this with stream readers and other methods but I would like to try to stay with the current code structure using ResourceBundle if possible. If anyone has any questions or needs clarification please feel free to let me know.

private final ResourceBundle bundle = ResourceBundle.getBundle("propertiesFile");

titleText = bundle.getString("title.text");

Just to give you an idea, title.text=会議参加者事前登録用ページ in my property file.

Any suggestions are appreciated. This has turned into a real pain.

Thanks again,

-Dave F.

3

There are 3 answers

2
Stephan On

when you run your app you can try this:

java -Dfile.encoding=UTF-8 -jar YourApp.jar

this basically sets the default charset

3
JFPicard On

ResourceBundle can only read ISO 8859-1 file. See this thread. How to use UTF-8 in resource properties with ResourceBundle

I had the same problem here, and I've converted my ResourceBundle to a Properties object like in the answer of this thread: read resourcebundle as UTF-8. getString() Method seems to change encoding to ISO-8859

Like this:

InputStream utf8in = getClass().getClassLoader().getResourceAsStream("/path/to/utf8.properties");
Reader reader = new InputStreamReader(utf8in, "UTF-8");
Properties props = new Properties();
props.load(reader);
0
MotoDave452 On

After some research I was actually still able to make use of the ResourceBundle method.

In my properties file that ResourceBundle pulls from, I listed the following in unicode:

title.text=\u4f1a\u8b70\u53c2\u52a0\u8005\u4e8b\u524d\u767b\u9332\u7528\u30da\u30fc\u30b8

When pulled in by ResourceBundle, it translates as:

会議参加者事前登録用ページ 

I'm sure this probably isn't the best practice, but it works without having to change how the entire project pulls in its resource properties, so I just thought I would share with you all for helping me out.