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

分类:java 2020-09-11T18:31:38    162人阅读   

一、将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") 当前工程路径



点击广告?(赏赐笔者)