如何用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内容转换为只读模式的方法。动态元素的使用显而易见,允许随时轻松修改。
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP