java.util.Properties 和 Cp1250
我可以使用与默认编码不同的 java.util.Properties 吗?
Can I use java.util.Properties with encoding different then default?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,但是你必须小心使用
load()
和store()
采用Reader 的方法
/Writer
,并使用具有正确编码的InputStreamReader/OutputStreamWriter
显式构造它们。对于隐式使用属性文件的库来说,这可能是不可能的。
编辑:上面描述的方法仅在 Java 1.6 中引入 - 对于旧版本,你运气不好,正如 dsadinoff 所写的。
Yes, but then you have to be careful to use the
load()
andstore()
methods that take aReader
/Writer
, and explicitly construct those by using anInputStreamReader/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.
除非你
这是规范中一个非常烦人的缺陷。有几种解决方法,最简单的可能是从适当的编码(cp1250、utf-8 等)源自动生成符合 unicode 转义的 .properties 文件。
Java 附带了一个名为 native2ascii 的转码器为您执行此操作:
有一些关于此主题的旧 RFE:
Not unless you
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:
如果您的属性文件在构建时可用,您还可以使用 native2ascii 任务在 ant 脚本中转换它:
If your properties file is available at build time, you can also convert it in your ant script using the native2ascii task: