原创 

JAVA 将Properties对象写为XML文件以及将XML读为Properties

分类:java    656人阅读    IT小君  2020-09-11 18:31

一、将Properties对象保存为XML文件

1、我们可以使用java.util.Properties.storeToXML()方法,他有如下两个重载:

public void storeToXML(OutputStream os,
                       String comment)
                throws IOException
        
等同于 props.storeToXML(os, comment, "UTF-8");.
 
public void storeToXML(OutputStream os,
                       String comment,
                       String encoding)
                throws IOException

2、源代码 xml 文件转Properties对象

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Properties;
 
public class JavaWriteProperties2XMLFile {
  public static void main(String[] args) {
    Properties properties = new Properties();
    properties.put("db.username", "username");
    properties.put("db.password", "password");
    properties.put("db.driver", "org.postgresql.Driver");
    properties.put("db.url", "jdbc:postgresql://localhost/testdb");
    
    /*
     *   使用方法 1: Properties.storeToXML(OutputStream os, String comment) throws IOException
     */
    String propertiesFile = System.getProperty("user.dir") + "\\file.xml";
    try(OutputStream propertiesFileWriter = new FileOutputStream(propertiesFile)){
      properties.storeToXML(propertiesFileWriter, "save to properties file");
    }catch(IOException ioe){
      ioe.printStackTrace();
    }
    
    /*
     *   使用方法 2:  Properties.storeToXML(OutputStream os, String comment, String encoding) throws IOException
     */
    propertiesFile = System.getProperty("user.dir") + "\\file_1.xml";
    try(OutputStream propertiesFileWriter = new FileOutputStream(propertiesFile)){
      properties.storeToXML(propertiesFileWriter, "save to XML file with UTF-8 enconding", StandardCharsets.UTF_8.toString());
    }catch(IOException ioe){
      ioe.printStackTrace();
    }
  }
}

运行结果

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>save to properties file</comment>
<entry key="db.password">password</entry>
<entry key="db.url">jdbc:postgresql://localhost/testdb</entry>
<entry key="db.username">username</entry>
<entry key="db.driver">org.postgresql.Driver</entry>
</properties>


二、将读XML文件读为Properties对象

1、使用方法 java.util.Properties.loadFromXML():

public void loadFromXML(InputStream in)
                 throws IOException,InvalidPropertiesFormatException

2、源代码Properties对象转XML文件

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
 
public class JavaReadXMLFile2Properties {
  public static void main(String[] args) {
    Properties properties = new Properties();
    
    String propertiesFile = System.getProperty("user.dir") + "\\file.xml";
    try(InputStream inputStream = new FileInputStream(propertiesFile)){
      properties.loadFromXML(inputStream);
    }catch(IOException ioe) {
      ioe.printStackTrace();
    }
    
    properties.forEach((k, v) -> System.out.println(String.format("key = %s, value = %s", k, v)));
  }
}

    运行结果:

key = db.driver, value = org.postgresql.Driver
key = db.username, value = username
key = db.url, value = jdbc:postgresql://localhost/testdb
key = db.password, value = password


note:System.getProperty("user.dir") 当前工程路径



点击广告,支持我们为你提供更好的服务

jQuery右端悬浮带返回顶部特效

html5图标下拉搜索框自动匹配代码

HTML5 Canvas竖直流动线条背景动画特效

网页设计开发公司网站模板

css鼠标跟随文字模糊特效

中小型创意设计服务公司网站模板

canvas炫酷鼠标移动文字粒子特效

响应式太阳能能源公司网站模板

响应式咖啡饮品宣传网站模板

现代时尚家具公司网站模板

响应式时尚单品在线商城网站模板

html5 svg夜空中星星流星动画场景特效

html5 canvas进度条圆环图表统计动画特效

小众时尚单品在线电子商务网站模板

HTML5数字产品服务公司网站模板

html5 canvas彩色碎片组合球形旋转动画特效

HTML5现代家居装潢公司网站模板

js+css3抽奖转盘旋转点餐代码

有机水果蔬菜HTML5网站模板

css+js实现的颜色渐变数字时钟动画特效

点击广告,支持我们为你提供更好的服务
 工具推荐 更多»
点击广告,支持我们为你提供更好的服务