如何用Java建立和修改屬性檔案?
文字和XML格式的程式?
在Java中,專案的屬性檔案由基於文字的鍵值對組成,通常以.properties副檔名儲存。鍵值對內容逐行顯示,通常使用記事本、寫字板等建立。
屬性檔案是儲存重要和機密資料的寶貴儲存庫。在本文中,我們將探討使用Java程式建立屬性檔案的過程。此(java.util.Properties)包中的Properties類提供了多個實用程式儲存方法,這些方法可以方便地在以下格式儲存屬性:
文字格式 或
XML格式
文字格式建立
store()函式用於以文字格式儲存屬性。此函式將屬性表中的鍵值對儲存到OutputStream,其方式可以輕鬆地使用load方法(接受InputStream)載入到屬性表中。
宣告
//declaration for store() method public void store(OutputStream out,String comments)
引數
Out 指的是用於生成XML文件的輸出流。
Comments 作為屬性列表的解釋性文字。
返回值
此方法從與指定鍵關聯的屬性列表中檢索值,如果該值存在,則返回先前的值;如果不存在,則返回null。
示例
由於我們正在以文字格式建立屬性檔案,因此“應提供有效的檔案路徑位置”。
import java.io.IOException; import java.io.FileNotFoundException; import java.util.Properties; import java.io.FileOutputStream; public class TutorialsPoint { public static void main(String args[]) throws IOException,FileNotFoundException{ // Firstly we will create properties files Properties prop = new Properties(); //Testing.properties file is created in the current directory FileOutputStream fileOutputStream = new FileOutputStream( "Testing.properties"); //setting the values to the properties //like username and password prop.setProperty("username", "Tutorials Point"); prop.setProperty("password", "Success"); //we are using store() method as we are //storing properties into properties file in text format prop.store( fileOutputStream, "Creating Properties file in Text Format"); fileOutputStream.close(); } }
輸出
我們將看到一個在程式中提到的指定位置建立的檔案。如果未指定路徑,僅給出檔名,則它將下載到當前工作目錄

當我們開啟檔案時,我們將看到以下內容:
#Creating Properties file in Text Format #Sun May 28 15:58:54 IST 2023 password=Success username=Tutorials Point
除鍵值對之外,所有內容都被視為以#符號表示的註釋。
當屬性內容很少時,使用文字格式的屬性比較有利。
XML格式建立
XML是儲存重要敏感資訊的有效格式。可擴充套件標記語言 (XML) 有一套編碼文件的規則。為了以XML格式儲存屬性,我們需要使用storeToXML()方法。
storeToXML()方法生成一個XML文件,該文件表示此表中存在的全部屬性。
宣告
//declaration for storeToXML() method public void storeToXML(OutputStream os,String comment)
引數
Out 指的是用於生成XML文件的輸出流。
Comments 作為屬性列表的解釋性文字。
返回值
由於storeToXML()方法的返回型別是void,因此它不會返回值。
示例
由於我們正在以文字格式建立屬性檔案,因此“應提供有效的檔案路徑位置”。
import java.io.IOException; import java.io.FileNotFoundException; import java.util.Properties; import java.io.FileOutputStream; public class TutorialsPoint { public static void main(String args[]) throws IOException, FileNotFoundException{ // we will create properties files Properties prop = new Properties(); //Testing.properties is created in the current directory FileOutputStream fileOutputStream = new FileOutputStream( "Testing.xml"); //setting the values to the properties //like username and password prop.setProperty("username", "Tutorials Point"); prop.setProperty("password", "Success"); //we are using storeToXML() method as we are //storing properties into properties file in XML format prop.storeToXML( fileOutputStream, "Creating Properties file in XML Format"); fileOutputStream.close(); } }
輸出
我們將看到一個在程式中提到的指定位置建立的檔案。如果未指定路徑,僅給出檔名,則它將下載到當前工作目錄

當我們開啟檔案時,我們將看到以下內容:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> <properties> <comment>Creating Properties file in XML Format</comment> <entry key="password">Success</entry> <entry key="username">Tutorials Point</entry> </properties>
當屬性檔案包含大量且重要的資訊時,建議專門選擇XML格式。
有時我們難以閱讀XML格式,因此我們有一種將XML內容轉換為只讀模式的方法。下面的Java程式可以滿足此需求。
步驟
包含必要資訊的輸入XML檔案。
選擇儲存以文字格式輸出的目的地。
使用loadFromXmL()載入XmL檔案。
最後,我們將獲得上面看到的文字格式的輸出。
輸入檔案(Testing.xml)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> <properties> <comment>Creating Properties file in XML Format</comment> <entry key="password">Success</entry> <entry key="username">Tutorials Point</entry> </properties>
示例
import java.io.FileInputStream; import java.util.InvalidPropertiesFormatException; import java.util.Properties; import java.io.InputStream; import java.io.OutputStream; import java.io.FileOutputStream; import java.io.IOException; public class TutorialsPoint { public static void main(String[] args) throws InvalidPropertiesFormatException, IOException{ String resultantFile = "sample.properties"; String inputFile = "Testing.xml"; InputStream is = new FileInputStream(inputFile); OutputStream os = new FileOutputStream(resultantFile); Properties properties = new Properties(); properties.loadFromXML(is); properties.store( os, "Converted from Testing.xml"); } }
輸出

當我們開啟檔案時,我們將看到以下內容:
#Converted from Testing.xml #Sun May 28 16:46:03 IST 2023 password=Success username=Tutorials Point
結論
在本文中,我們瞭解瞭如何構建屬性檔案,還了解了一種將XML內容轉換為只讀模式的方法。動態元素的使用顯而易見,允許隨時輕鬆修改。