Properties类与*.properties或*.xml文件的联合使用

Properties代表了一个属性的持久化集合。它可以被保存为一个流,或者从一个流中加载。在属性列表中每个键和对应的值都是字符串。

一个属性列表能包含另外一个属性列表作为它的默认值。如果属性中键不能在原属性列表中找到的话,它就会去另一个属性列表中搜索。

因为Properties继承自Hashtable,所以put和putAll方法能用于一个Properties对象。

如果一个Properties对象是损坏的,save和store方法不能调用,并且propertyName和list方法会调用失败。

load(InputStream) 方法可以实现从一个*.properties中读取数据为Properties对象

store(OutputStream, String)方法可以实现把Properties对象转换成*.properties文件。

loadFromXML(InputStream)方法可以实现从一个*.xml文件中读取数据为Properties对象。

storeToXML(OutputStream, String, String)方法可以实现从把Properties对象转换为xml文件。

list(PrintStream out)方法可以打印Properites对象到指定的输出流,对于调试来说是非常有用的。

示例如下:

package com.anllin.properties;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;

public class PropTest
{
public static void main(String[] args) throws Exception
{
getConfigFromPropertiesFile();
System.out.println(
"---------------------------");
getConfigFormXMLFile();
}

private static void getConfigFromPropertiesFile() throws Exception
{
Properties props
= new Properties();
//把一个properties文件加载为Properties集合
props.load(new FileInputStream("src/jdbc.properties"));
System.out.println(props.getProperty(
"driverClassName"));
System.out.println(props.getProperty(
"url"));
System.out.println(props.getProperty(
"username"));
System.out.println(props.getProperty(
"password"));
System.out.println(props.getProperty(
"initialSize"));
System.out.println(props.getProperty(
"maxActive"));
System.out.println(props.getProperty(
"maxIdle"));
System.out.println(props.getProperty(
"minIdle"));
        //把Properties集合存储为一个properties文件
props.store(new FileOutputStream("src/jdbcProp.properties"),"jdbc properties");
}

private static void getConfigFormXMLFile() throws Exception
{
Properties props
= new Properties();
//把一个XML文件加载为Properties集合
props.loadFromXML(new FileInputStream("src/jdbc.xml"));
System.out.println(props.getProperty(
"driverClassName"));
System.out.println(props.getProperty(
"url"));
System.out.println(props.getProperty(
"username"));
System.out.println(props.getProperty(
"password"));
System.out.println(props.getProperty(
"initialSize"));
System.out.println(props.getProperty(
"maxActive"));
System.out.println(props.getProperty(
"maxIdle"));
System.out.println(props.getProperty(
"minIdle"));
//把Properties集合存储为一个XML文档
props.storeToXML(new FileOutputStream("src/jdbcProps.xml"),"jdbc properties","utf-8");
}
}

jdbc.properties

driverClassName=org.gjt.mm.mysql.Driver
url
=jdbc\:mysql\://localhost\:3306/test?useUnicode\=true&characterEncoding\=UTF-8
username
=root
password
=123
initialSize
=1
maxActive
=500
maxIdle
=2
minIdle
=1

 jdbc.xml(必须是以下这种格式)

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
  <comment>jdbc properties</comment>
  <entry key="maxActive">500</entry>
  <entry key="password">123</entry>
  <entry key="url">jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8</entry>
  <entry key="driverClassName">org.gjt.mm.mysql.Driver</entry>
  <entry key="minIdle">1</entry>
  <entry key="initialSize">1</entry>
  <entry key="maxIdle">2</entry>
  <entry key="username">root</entry>
</properties>

properties.dtd(jdbc.xml的验证文件,符合它才是有效的)

<?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>
用props.list(System.out);可以得到如下的输出
-- listing properties --
url=jdbc:mysql://localhost:3306/test?useU...
password=123
maxActive=500
driverClassName=org.gjt.mm.mysql.Driver
initialSize=1
minIdle=1
username=root
maxIdle=2
原文地址:https://www.cnblogs.com/zfc2201/p/2150055.html