This week's book giveaways are in the AI and JavaScript forums.
We're giving away four copies each of GANs in Action and WebAssembly in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Please help with Chinese encoding

 
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, this is my first experience with Chinese.

I created a Properties file where there is a line

Landing=着陆

(Chinese speakers, please don't laugh, I got that at a dictionary).

I saved that in utf-8 using Notepad.

Now, I am reading that using a Reader:

Properties p = new Properties().load(new FileReader(fileName));
String translation = p.getProperty("Landing");
String encoded = new String(key.getBytes("ISO-8859-1"), "UTF-8");

Results:

translation = �陆
encoded = �??�??

What am I doing wrong?





 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I created a Properties file where there is a line

Landing=着陆

What am I doing wrong?


Creating a properties file where there is a line with Chinese.

Properties files must be created using native2ascii or similar tool.

See the Properties docs here. There are many tutorials and references regarding I18N available on the web.
 
Jane Jukowsky
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

David Newton wrote:
Properties files must be created using native2ascii or similar tool.

See the Properties docs here. There are many tutorials and references regarding I18N available on the web.



I don't see that in the docs. Here is what the docs say:

* The {@link #load(java.io.InputStream) load(InputStream)} /
* {@link #store(java.io.OutputStream, java.lang.String) store(OutputStream, String)}
* methods work the same way as the load(Reader)/store(Writer, String) pair, except
* the input/output stream is encoded in ISO 8859-1 character encoding.

* Characters that cannot be directly represented in this encoding can be written using
* Unicode escapes
* ; only a single 'u' character is allowed in an escape
* sequence. The native2ascii tool can be used to convert property files to and
* from other character encodings.

What I hear it say is, load(Reader) ought to be able to load Unicode, unlike load(Stream), no?
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fourth paragraph:


The load and store methods load and store properties in a simple line-oriented format specified below. This format uses the ISO 8859-1 character encoding. Characters that cannot be directly represented in this encoding can be written using Unicode escapes ; only a single 'u' character is allowed in an escape sequence. The native2ascii tool can be used to convert property files to and from other character encodings.


All I know is what actually works for me, which precludes what you're trying to do.

Give it a shot.
 
Jane Jukowsky
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did it!! Here is how you load (and save) UTF-8 Properties. No conversion to ASCII needed.





The resulting Properties file:


 
Rancher
Posts: 3418
34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For what it's worth, load(Reader) was added in JDK 1.6. David Newton's "fourth paragraph" quote above seems to be from an earlier JDK, hopefully JDK 1.5. That fourth paragraph changes substantially with the newer release, which is why you two had such different interpretations.

Moral: the JDK version can be important. Things change. Use the most recent version you can, but be aware that if the method you're talking about has a note like "since 1.6" or "since 1.7", it may not be familiar to some programmers. So tell us what version you're using, to minimize confusion.
 
Jane Jukowsky
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
LMAO,good catch Mike! Point taken.

Thank you David.
 
Why am I so drawn to cherry pie? I can't seem to stop. Save me tiny ad!
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!