📜  如何在文本和 XML 格式的Java程序中创建和修改属性文件?

📅  最后修改于: 2022-05-13 01:54:20.217000             🧑  作者: Mango

如何在文本和 XML 格式的Java程序中创建和修改属性文件?

属性文件是一个面向文本的键值,是存在于具有.properties扩展名的Java项目中的一对内容。内容是逐行键值对的,通常是通过记事本、写字板、EditPlus等方式准备的,属性文件通常有助于存储重要的敏感信息,在本文中,让我们看看如何使用Java程序创建属性文件。

Java API 有Java.util.Properties类,它有几个实用程序 store ()方法来存储文本或 XML 格式的属性。为了以文本格式存储属性,可以使用Store()方法。 storeToXML () 用于使 XML格式。

store() 方法有两个参数,如输出流和注释。

文本格式创建:

让我们看看如何以文本格式创建属性文件。当我们创建属性内容时,需要给出一个有效的文件路径位置。



Java
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
  
public class CreationOfTextOrientedProperties {
  
    public static void main(String args[])
        throws FileNotFoundException, IOException
    {
  
        // Creating properties files from Java program
        Properties properties = new Properties();
        
        // In the name of userCreated.properties, in the
        // current directory location, the file is created
        FileOutputStream fileOutputStream
            = new FileOutputStream(
                "userCreated.properties");
  
        // As an example, given steps how
        // to keep username and password
        properties.setProperty("username", "value1");
        properties.setProperty("password", "value2");
  
        // writing properites into properties file
        // from Java As we are writing text format,
        // store() method is used
        properties.store(
            fileOutputStream,
            "Sample way of creating Properties file from Java program");
  
        fileOutputStream.close();
    }
}


Java
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
  
public class CreationOfXMLOrientedProperties {
  
    public static void main(String args[])
        throws FileNotFoundException, IOException
    {
  
        // Creating properties files from Java program
        Properties properties = new Properties();
  
        // In the name of userCreated.xml, in the current
        // directory location, the file is created
        FileOutputStream fileOutputStream
            = new FileOutputStream("userCreated.xml");
  
        // As an example, given steps how to keep username
        // and password
        properties.setProperty("username", "value1");
        properties.setProperty("password", "value2");
  
        // writing properites into properties file
        // from Java As we are writing in XML format,
        // storeToXML() method is used
        properties.storeToXML(
            fileOutputStream,
            "Sample way of creating Properties file from Java program");
  
        fileOutputStream.close();
    }
}


Java
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;
  
public class ConvertXMLToTextOrientedProperties {
    public static void main(String[] args)
        throws InvalidPropertiesFormatException, IOException
    {
        String outputPropertiesFile
            = "sampleapplication.properties";
        String inputXmlFile
            = "sampleapplicationProperties.xml";
  
        // Input XML File which contains
        // necessary information
        InputStream inputStream
            = new FileInputStream(inputXmlFile);
  
        // Output properties File
        OutputStream outputStream
            = new FileOutputStream(outputPropertiesFile);
  
        Properties properties = new Properties();
  
        // Load XML file that has necessary information
        properties.loadFromXML(inputStream);
  
        // Store to properties file via this way
        properties.store(
            outputStream,
            "Converted from sampleapplicationProperties.xml");
  
        // For sample testing let us get username--It is
        // nothing but "Geek"
  
        // As it is converted to .properties file,
        // we can get the values in this way
        System.out.println(properties.get("username"));
    }
}


XML



Elegant way of converting sampleapplicationProperties.xml 
  to Sampleapplication.properties
Geek
  XXXXWeldoneXXXX


输出:

我们正在使用 store() 以文本格式保存属性文件。作为键值对,它们被设置。即键 = 值。除此之外,所有都被视为注释,因此带有# 符号,注释被放置。

当属性内容较少,开发团队经常变化,最终用户在非 IT 方面时,使用文本格式的属性在许多情况下很有帮助。

XML 格式创建:

在许多情况下,需要 XML 提供一种易于理解且高效的格式来存储重要的敏感信息。可扩展标记语言 (XML) 是一种标记语言,它具有一组用于编码文档的规则,并且它们以人类可读和机器可读的格式都可以理解。在这里,让我们看看如何通过Java程序创建

Java

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
  
public class CreationOfXMLOrientedProperties {
  
    public static void main(String args[])
        throws FileNotFoundException, IOException
    {
  
        // Creating properties files from Java program
        Properties properties = new Properties();
  
        // In the name of userCreated.xml, in the current
        // directory location, the file is created
        FileOutputStream fileOutputStream
            = new FileOutputStream("userCreated.xml");
  
        // As an example, given steps how to keep username
        // and password
        properties.setProperty("username", "value1");
        properties.setProperty("password", "value2");
  
        // writing properites into properties file
        // from Java As we are writing in XML format,
        // storeToXML() method is used
        properties.storeToXML(
            fileOutputStream,
            "Sample way of creating Properties file from Java program");
  
        fileOutputStream.close();
    }
}

输出:



如果我们检查 XML 的输出,它的条目的打开和关闭是相等的。

通过Java程序创建的也具有相同的结构。它以 开头,以 结尾。除了键值对集之外,文本被视为注释,因此它们位于注释标签内。属性文件只有键值,这里它也在“entry”标签中与“key=”一起声明,这意味着为每个键值对给出单独的条目。

每当属性文件内容很大并且包含银行交易、财务数据等敏感信息时,最好只使用 XML 格式。

一种将 XML 内容转换为只读文本模式的便捷方式

Java

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;
  
public class ConvertXMLToTextOrientedProperties {
    public static void main(String[] args)
        throws InvalidPropertiesFormatException, IOException
    {
        String outputPropertiesFile
            = "sampleapplication.properties";
        String inputXmlFile
            = "sampleapplicationProperties.xml";
  
        // Input XML File which contains
        // necessary information
        InputStream inputStream
            = new FileInputStream(inputXmlFile);
  
        // Output properties File
        OutputStream outputStream
            = new FileOutputStream(outputPropertiesFile);
  
        Properties properties = new Properties();
  
        // Load XML file that has necessary information
        properties.loadFromXML(inputStream);
  
        // Store to properties file via this way
        properties.store(
            outputStream,
            "Converted from sampleapplicationProperties.xml");
  
        // For sample testing let us get username--It is
        // nothing but "Geek"
  
        // As it is converted to .properties file,
        // we can get the values in this way
        System.out.println(properties.get("username"));
    }
}

输入文件 (sampleapplicationProperties.xml)

XML




Elegant way of converting sampleapplicationProperties.xml 
  to Sampleapplication.properties
Geek
  XXXXWeldoneXXXX

生成的输出文件(sampleapplication.properties)

和我们一样, System.out.println(properties.get(“username”)); ,它显示

“Geek”作为输出。因此 loadFromXML() 有助于加载 XML 文件并通过 store() 将其转换为面向文本的属性文件,并且在转换后,我们可以轻松获取属性值。

结论 :

在本文中,我们已经看到了从Java程序创建属性文件的方法。尽情享受它们吧。它们在软件项目的任何部分都有帮助,因为属性文件是保存敏感信息的关键文件,而且它们以键值对的形式作为文本或 XML 格式,可以看到动态使用,并且在任何时间点,我们也可以轻松修改它们。