How To Utilisation As Well As Alter Properties File Class Coffee Plan Inwards Text As Well As Xml Format

Though most of the fourth dimension nosotros exercise in addition to alter properties file using text editor similar notepad, word-pad or edit-plus, It’s every bit good possible to exercise in addition to edit properties file from Java program. Log4j.properties, which is used to configure Log4J based logging inward Java in addition to jdbc.properties which is used to specify configuration parameters for database connectivity using JDBC are 2 most mutual instance of holding file inward Java. Though I convey non flora whatever existent province of affairs where I take away to exercise properties file using Java computer program only it’s ever proficient to know close facilities available inward Java API. In final Java tutorial on Properties nosotros convey seen how to read values from properties file on both text in addition to XML format in addition to inward this article nosotros volition come across how to exercise properties file on both text in addition to XML format. Java API’s java.util.Properties flat provides several utility store() methods to shop properties inward either text or xml format. Store() tin endure used to shop holding inward text properties file in addition to  storeToXML() method tin endure used for creating a Java holding file inward XML format.

Java computer program to exercise in addition to shop Properties inward text in addition to XML format

Though most of the fourth dimension nosotros exercise in addition to alter properties file using text editor similar notepa How to Create in addition to Modify Properties File Form Java Program inward Text in addition to XML FormatAs I said before java.util.Properties correspond holding file inward Java program. It provides load() in addition to store() method to read in addition to write properties files from in addition to to File system. Here is uncomplicated instance of creating Java holding file cast computer program itself.


import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * Java computer program to exercise in addition to alter holding file inward text format in addition to storing
 * holding on it. Most of the fourth dimension nosotros travel text editor to exercise in addition to edit holding
 * file e.g. jdbc.properties or log4j.properties only nosotros tin every bit good exercise holding
 * file from Java computer program every bit shown inward this example.
 *
 * @author Javin Paul
 */

public class TextPropertyWriter {

    public static void main(String args[]) throws FileNotFoundException, IOException {

        //Creating properties files from Java program
        Properties props = new Properties();
        FileOutputStream fos = new FileOutputStream("c:/user.properties");
     
        props.setProperty("key1", "value1");
        props.setProperty("key2", "value2");
     
        //writing properites into properties file from Java
        props.store(fos, "Properties file generated from Java program");
     
        fos.close();
     
    }
}

This volition exercise user.properties file inward C:\. hither is how that holding file volition hold back like:

#Properties file generated from Java program
#Mon January xvi 03:00:57 VET 2012
key2=value2
key1=value1

Here is about other instance of creating Java holding file inward XML format from Java program:

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * Java computer program to shop properties inward XML holding file. stroeToXML() method of
 * java.util.Properties flat is used to salve properties inward XML holding file from Java
 * program.
 *
 * @author Javin Paul
 */

public class XmlPropertiesWriter {

    public static void main(String args[]) throws FileNotFoundException, IOException {

        //Reading properties files inward Java example
        Properties props = new Properties();
        FileOutputStream fos = new FileOutputStream("c:/user.xml");
     
        props.setProperty("key1", "value1");
        props.setProperty("key2", "value2");
     
        //writing properites into properties file from Java
        props.storeToXML(fos, "Properties file inward xml format generated from Java program");
     
        fos.close();

     
    }
}
Output:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Properties file inward xml format generated from Java program</comment>
<entry key="key2">value2</entry>
<entry key="key1">value1</entry>
</properties>

That’s all on How to exercise Properties file from Java computer program or How to alter Properties from Java program. As I said almost all fourth dimension nosotros travel text editor e.g. notepad or notepad++ to edit properties file similar log4j.properties or jdbc.properties, you lot tin every bit good edit them from Java computer program if needed. I personally prefer properties file inward text format if release of properties is non much in addition to XML based properties file if file is big plenty e.g. if you lot convey many properties to leverage XML editors.

Further Learning
Java In-Depth: Become a Complete Java Engineer!
Master Java Web Services in addition to REST API amongst Spring Boot
How to read XML files using SAX parser inward Java

Belum ada Komentar untuk "How To Utilisation As Well As Alter Properties File Class Coffee Plan Inwards Text As Well As Xml Format"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel