想必大家都用过*.properties文件,作为配置文件。但是,如果该文件写入了中文,待编译后内容就会成为乱码,使用native命令也好、使用ant执行编码转换也好,多少有点麻烦,与其如此,我们不如直接使用properties的xml格式。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> <properties> <comment>logoLocation</comment> <entry key="logoLocation"> <![CDATA[/image/logo/]]> </entry> <entry key="mail.host"> <![CDATA[webmaster@zlex.org]]> </entry> <entry key="site.name"> <![CDATA[zlex中文网站]]> </entry> </properties>
对应原有的properties文件
#logoLocation logoLocation=/image/logo/ mail.host=webmaster@zlex.org site.name=zlex中文网站
读取\存储:
Properties.loadFromXML(InputStream in)
Properties.storeToXML(OutputStream os, String comment, String encoding)
本文介绍了解决*.properties文件中中文乱码的问题,推荐使用XML格式替代传统的格式,并提供了具体的XML配置示例。此外还介绍了如何使用Java API进行读取和存储。
610

被折叠的 条评论
为什么被折叠?



