欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

包含中文的properties文件,第一行要空出来

程序员文章站 2022-04-25 14:29:19
...

项目的配置文件中包含了中文,文件的编码格式为UTF-8.

当读取properties文件时第一个Key总是失败,后面的Key则正常。

Properties类API(http://docs.oracle.com/javase/7/docs/api/java/util/Properties.html)中提到如下一句话

The load(Reader) / store(Writer, String) methods load and store properties from and to a character based stream in a simple line-oriented format specified below. The load(InputStream) / 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 as defined in section 3.3 of The Java™ Language Specification; 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.

大体意思是, properties文件是基于行来读取和存储键值对的, 而期望的输入/输出流字符编码是ISO 8859-1. 那些不能使用该方式存储的字符集呢, 需要使用native2ascii命令对property文件进行转码.

解决方案为:在properties文件的首先加为空行(或写注释也可以)

参考:http://blog.csdn.net/pansanday/article/details/49895399

相关标签: java properties