想必大家都用过*.properties文件,作为配置文件。但是,如果该文件写入了中文,待编译后内容就会成为乱码,使用native命令也好、使用ant执行编码转换也好,多少有点麻烦,与其如此,我们不如直接使用properties的xml格式。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>系统配置</comment>
<entry
key="logo.location"><![CDATA[/image/logo/]]></entry>
<entry
key="mail.host"><![CDATA[webmaster@zlex.org]]></entry>
<entry
key="site.name"><![CDATA[zlex中文网站]]></entry>
<entry
key="welcome"><![CDATA[欢迎您,{0}!]]></entry>
</properties>
对应原有的properties文件
#系统配置
logo.location=/image/logo/
mail.host=webmaster@zlex.org
site.name=zlex中文网站
welcome=欢迎您,{0}!
这里需要替换{0},可以使用MessageFormat,参考如下代码:
private FileInputStream fis;
@Before
public void init() {
try {
fis = new FileInputStream(new File("config.xml"));
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
@Test
public void t() {
Properties properties = new Properties();
try {
properties.loadFromXML(fis);
System.err.println(MessageFormat.format(
(String) properties.get("welcome"), "snowolf"));
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
得到控制台输出:
引用
欢迎您,snowolf!
![](/images/smiles/icon_biggrin.gif)
因为使用XML格式,不受系统编译影响,不存在中文问题!![](/images/smiles/icon_biggrin.gif)
分享到:
相关推荐
这是一个简单实现读取properties、xml格式的配置文件的小案例。虽然实际项目中可能不是这样实现的。作为了解也是不错的。 一、读取properties类型文件 方法一:java.util.ResourceBundle读取... dom4j读取xml文件;
本文侧重于了解自定义properties文件,以及properties中的数据格式,在gradle中如何使用等知识点。 一、概述 在Android Studio 创建一个项目的时候,rootProject下面会生成gradle.properties和local.properties文件...
JAVA Properties xml 读写操作
js读取properties文件,很好用的。js读取properties文件,很好用的。js读取properties文件,很好用的。js读取properties文件,很好用的。
NULL 博文链接:https://it586.iteye.com/blog/1676577
java写的,用来比较properties文件的工具
读取properties文件返回map
properties文件读写操作
ConfigFile configfile = ConfigFile.getInstance("ipConfig123.properties"); String ip = configfile.getkeyvalue("ip"); 可以取出ipConfig123.properties 文件中IP的内容
java 改变Properties文件中的键值 java 改变Properties文件中的键值
进行properties文件的批量处理工具,能够用来改动部分参数而进行的文本提取修改工具。
Properties 文件文中乱码问题解决 Properties 文件文中乱码问题解决是指在使用 Struts 框架进行国际化操作时,对于中文资源文件的处理问题。在 Properties 文件中,中文字符可能会变成乱码,这是因为 Eclipse 中的...
主要介绍了Python实现读取Properties配置文件的方法,结合实例形式分析了Python读取Properties配置文件类的定义与使用相关操作技巧,需要的朋友可以参考下
eclipse下properties文件编辑插件
此工具类只用于Java后端在操作Properties文件的时候写的工具类,方便properties文件的存取操作
spring-mvc.xml spring-mybatis.xml web.xml log4j.properties,项目中需要用到的配置文件。直接可用。
设置Eclipse中properties文件打开方式myeclipse一样有source和properties两个视图方法
Apache Commons Configuration对properties、xml配置文件的【增删改查】及动态加载小案例。小编也是找了网上很多资源,综合研究了一下。被怪我黑!!!
springboot-dubbo整合的两种实现方式(properties和xml方式),还有一种@Configuration注解方式没有进行实现,麻烦,尤其到后期进行一些dubbo服务的配置管理不太直观,所以建议使用xml方式,而且spring-dubbo工程转成...
eclipse-properties文件插件 问题 在Struct2在读取语言包文件language.properties属性文件,如果文件中包含中文,会出现乱码。虽然可以native2ascii进行解决,但是使用起来不方便,为了解决这个问题,可以为Eclipse...