JAVA – Properties檔案寫出與讀取

撰寫程式的過程中,不免需要使用到一些設定檔,讓程式執行更有彈性,盡可能避免Hard Code的情形發生。於Java當中JR常用Properties檔案作為設定檔使用,Properties類別繼承了HashTable類別,以key, value的方式儲存資料,用來讀取設定檔特別好用。除了讀取之外,在些許情況可能需要透過程式將資料寫成Properties檔案,進行再次利用。這種情形JR確實遇到過,因此將Properties讀取與寫出的方法做成紀錄,以後方便回來查看,即可馬上上手使用。附帶一提的是Properties類別不僅可以寫出與讀取properties也可以對xml進行處理。

程式實作

Java程式:PropertiesWriteAndReadDemo.java

package cc.jren;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.OutputStreamWriter;
import java.text.MessageFormat;
import java.util.Properties;
import java.util.logging.Logger;

/**
 * JAVA - Properties檔案寫出與讀取
 *
 * @author JR
 * @Date 2022年4月16日
 */
public class PropertiesWriteAndReadDemo {

	private static final Logger LOGGER = Logger.getGlobal();

	public static void main(String[] args) throws Exception {
		LOGGER.info("寫出 Properties File");
		writePropertiesFile();

		LOGGER.info("讀取 Properties File");
		readPropertiesFile();
	}

	/**
	 * 寫出 Properties File
	 * 
	 * @throws Exception
	 */
	private static void writePropertiesFile() throws Exception {
		var properties = new Properties();
		properties.setProperty("website_name", "JR's Blog");
		properties.setProperty("website_description", "我的跑龍套生活");
		properties.setProperty("website_url", "https://jren.cc/");

		// 寫出 properties 檔案
		var fos = new FileOutputStream("target/jrencc.properties", false);
		properties.store(new OutputStreamWriter(fos, "UTF-8"), "https://jren.cc/");
		LOGGER.info("寫出檔案: target/jrencc.properties");

		// 寫出 XML 檔案
		var fosXml = new FileOutputStream("target/jrencc.xml", false);
		properties.storeToXML(fosXml, "https://jren.cc/");
		LOGGER.info("寫出檔案: target/jrencc.xml");
	}

	/**
	 * 讀取 Properties File
	 * 
	 * @throws Exception
	 */
	private static void readPropertiesFile() throws Exception {
		LOGGER.info("讀取 Properties File - jrencc.properties");
		var properties = new Properties();
		properties.load(new FileReader("target/jrencc.properties"));

		System.out.println(MessageFormat.format("website_name: {0}", properties.getProperty("website_name")));
		System.out.println(
				MessageFormat.format("website_description: {0}", properties.getProperty("website_description")));
		System.out.println(MessageFormat.format("website_url: {0}", properties.getProperty("website_url")));

		LOGGER.info("讀取 Properties File - jrencc.xml");
		var propertiesXml = new Properties();
		propertiesXml.loadFromXML(new FileInputStream("target/jrencc.xml"));
		System.out.println(MessageFormat.format("website_name: {0}", propertiesXml.getProperty("website_name")));
		System.out.println(
				MessageFormat.format("website_description: {0}", propertiesXml.getProperty("website_description")));
		System.out.println(MessageFormat.format("website_url: {0}", propertiesXml.getProperty("website_url")));

	}

}

執行結果

4月 16, 2022 9:26:03 下午 cc.jren.PropertiesWriteAndReadDemo main
資訊: 寫出 Properties File
4月 16, 2022 9:26:03 下午 cc.jren.PropertiesWriteAndReadDemo writePropertiesFile
資訊: 寫出檔案: target/jrencc.properties
4月 16, 2022 9:26:03 下午 cc.jren.PropertiesWriteAndReadDemo writePropertiesFile
資訊: 寫出檔案: target/jrencc.xml
4月 16, 2022 9:26:03 下午 cc.jren.PropertiesWriteAndReadDemo main
資訊: 讀取 Properties File
4月 16, 2022 9:26:03 下午 cc.jren.PropertiesWriteAndReadDemo readPropertiesFile
資訊: 讀取 Properties File - jrencc.properties
website_name: JR's Blog
website_description: 我的跑龍套生活
website_url: https://jren.cc/
4月 16, 2022 9:26:03 下午 cc.jren.PropertiesWriteAndReadDemo readPropertiesFile
資訊: 讀取 Properties File - jrencc.xml
website_name: JR's Blog
website_description: 我的跑龍套生活
website_url: https://jren.cc/
Tags:

Add a Comment

發佈留言必須填寫的電子郵件地址不會公開。