简体   繁体   中英

java.util.Properties and Cp1250

我可以使用java.util.Properties编码不同的默认值吗?

Yes, but then you have to be careful to use the load() and store() methods that take a Reader / Writer , and explicitly construct those by using an InputStreamReader/OutputStreamWriter with the correct encoding.

This may not be possible with libraries that use properties files implicitly.

Edit: The methods described above have only been introduced in Java 1.6 - for older versions, you're out of luck, as dsadinoff wrote.

Not unless you

  1. are running java 6 or later
  2. control the code loading the properties file, and can use a Reader. See the javadoc .

This is a pretty annoying flaw in the spec. There are several workarounds, probably the simplest being to auto-generate a unicode-escaped compliant .properties file from an encoding-appropriate (cp1250, utf-8, whatever) source.

Java ships with a transcoder called native2ascii to do this for you:

There are some aged RFEs on this subject:

If your properties file is available at build time, you can also convert it in your ant script using the native2ascii task:

<property name="javac.source.encoding" value="Cp1250"/>

<native2ascii src="${src.dir}" dest="${classes.dir}"
   encoding="${javac.source.encoding}"
   includes="**/*.properties"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM