JAVA Properties类加载.properties与.xml配置文件
来源:互联网 发布:门店经营数据分析 编辑:程序博客网 时间:2024/05/09 18:20
Tiger系列一:从XML中加载属性
1、基本加载属性的方法
l Sample属性文件:sample.properties
foo=bar
fu=baz
l 加载属性的Sample程序
import java.io.FileInputStream;import java.util.Properties; public class LoadSampleProperties { public static void main(String[] args) throws Exception { Properties prop = new Properties(); FileInputStream fis = new FileInputStream("props/sample.properties"); prop.load(fis); prop.list(System.out); System.out.println("\nThe foo property: " + prop.getProperty("foo")); }}
l 输出结果如下:
-- listing properties --
fu=baz
foo=bar
The foo property: bar
2、从XML中加载属性
l 下面是Properties DTD清单:
<?xml version="1.0" encoding="UTF-8"?><!-- DTD for properties --><!ELEMENT properties ( comment?, entry* ) ><!ATTLIST properties version CDATA #FIXED "1.0"><!ELEMENT comment (#PCDATA) ><!ELEMENT entry (#PCDATA) ><!ATTLIST entry key CDATA #REQUIRED>
l Sample XML属性文件:sample.xml(符合上面的Properties DTD)<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"><properties> <comment>Hello</comment> <entry key="foo">bar</entry> <entry key="fu">baz</entry></properties>
l <entry>标记指定具体一个属性,由key属性指定属性名,而<entry>标记的内容指定属性值
l <comment>标记可以用来指定注释
l 从XML文件加载属性的Sample程序
import java.io.FileInputStream;import java.util.Properties; public class LoadSampleXML { public static void main(String[] args) throws Exception { Properties prop = new Properties(); FileInputStream fis = new FileInputStream("props/sample.xml"); prop.loadFromXML(fis); prop.list(System.out); System.out.println("\nThe foo property: " + prop.getProperty("foo")); }}
l 输出的结果是一样的
l 可以看出方法很简单:使用XML文件来保存属性,使用loadFromXML()方法替代原来的load()方法来加载XML文件中属性
3、更新XML文件中的属性值
l Sample程序
import java.io.FileInputStream;import java.io.FileOutputStream;import java.util.Properties; public class UpdateSampleXml { public static void main(String[] args) throws Exception { Properties prop = new Properties(); FileInputStream fis = new FileInputStream("props/sample.xml"); prop.loadFromXML(fis); prop.list(System.out); System.out.println("\nThe foo property: " + prop.getProperty("foo")); prop.setProperty("foo", "Hello World!"); prop.setProperty("new-name", "new-value"); FileOutputStream fos = new FileOutputStream("props/sample.xml"); prop.storeToXML(fos, "Store Sample"); fos.close(); fis = new FileInputStream("props/sample.xml"); prop.loadFromXML(fis); prop.list(System.out); System.out.println("\nThe foo property: " + prop.getProperty("foo")); }}
l 上面的例子加载了sample.xml中的属性,更新了foo属性的值,并新加了new-name属性,调用storeToXML()方法保存到原文件中,并改变注释内容为Store Sample
l 程序执行后的sample.xml的内容如下:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"><properties><comment>Store Sample</comment><entry key="new-name">new-value</entry><entry key="fu">baz</entry><entry key="foo">Hello World!</entry></properties>
- JAVA Properties类加载.properties与.xml配置文件
- java加载.properties配置文件
- Java开发-读取XML与properties配置文件
- java 配置文件properties自动加载类
- JAVA 动态加载properties配置文件
- java读取xml/.properties配置文件
- Properties类与配置文件
- Properties类与配置文件
- 加载properties配置文件工具类
- properties和xml配置文件加载效率
- Spring Xml配置文件自动加载properties文件
- ApplicationContext.xml中加载properties配置文件
- log4j的使用与java中properties配置文件加载
- log4j的使用与java中properties配置文件加载
- JAVA Servlet 动态加载配置文件.properties
- 问题:使用java中properties加载配置文件
- java 加载properties配置文件属性到常量
- spring加载properties配置文件
- 终于解决了IE里的Out of memory问题!
- Java Web获取Web应用根路径
- 设计模式C++(Strategy策略模式)
- mysql 字符串处理
- Ext.form.DateField简单用法及日期范围控制
- JAVA Properties类加载.properties与.xml配置文件
- Window Driver 安装
- 敏捷个人微刊封面及敏捷个人使命和加入社区方式
- C# 动态编译代码
- 驱动安装包制作:使用SetupCopyOEMInf函数 用installshield 即可完成安装
- Linux 系统内核空间与用户空间通信的实现与分析
- 我看AMD推土机FX-4170和INTEL第三代I3 3220
- 39.黑马程序员-基本数据类型对象包装类
- eclipse的js文件中汉字是乱码怎么解决,更改eclipse的编码方式