2

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.

MotoDave452
  • 398
  • 1
  • 8
  • 22
  • First of all you need to encode the `.properties` file as UTF-8 and then store the characters using its UTF-8 encode representation...I tried that before with Spanish characters and it worked like that, so it should be similar with Japanese characters – x80486 Jun 19 '15 at 15:40

3 Answers3

1

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);
Community
  • 1
  • 1
JFPicard
  • 5,029
  • 3
  • 19
  • 43
  • Thank you very much for your help. This seems like a viable way of proceeding. If I may ask, once I get a handle to my properties file and encode the file using the above method, how would I get a handle to the **title.text** property within my properties file and assign its value to a string that I can then make use of? – MotoDave452 Jun 19 '15 at 16:02
  • Just do props.getProperty("title.text"). Also, you can put this code in a class to not repeat it each time you want a localized string. – JFPicard Jun 19 '15 at 16:14
  • Since JDK9 Java expects UTF-8 for .properties. See PropertyResourceBundle javadoc for more info. – jjazzboss Dec 21 '20 at 22:45
1

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.

MotoDave452
  • 398
  • 1
  • 8
  • 22
0

when you run your app you can try this:

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

this basically sets the default charset

Stephan
  • 8,000
  • 3
  • 36
  • 42