Monday, May 6, 2024
HomeJavaEasy methods to Create and Modify Properties File Kind Java Program in...

Easy methods to Create and Modify Properties File Kind Java Program in Textual content and XML Format


Disclosure: This text could comprise affiliate hyperlinks. Whenever you buy, we could earn a small fee.

Although more often than not we create and modify properties file utilizing textual content editors like notepad, word-pad or edit-plus, It’s additionally potential to create and edit
properties file from Java program.
Log4j.properties, which is
used to configure Log4J based mostly
logging
in Java
and jdbc.properties which is used to specify configuration
parameters for database
connectivity utilizing JDBC
are two commonest instance of property file in
Java. Although I’ve not discovered any actual scenario the place I must create
properties file utilizing Java program nevertheless it’s at all times good to learn about
services obtainable in Java API. 
In final Java tutorial on Properties we have now
seen
how
to learn values from properties file
on each textual content and XML format and on this article we’ll see the right way to create properties file on each textual content and XML format.

Java API’s java.util.Properties class supplies a number of utility retailer() strategies to
retailer properties in both textual content or xml format.
Retailer() will be
used to retailer property in textual content properties file and  
storeToXML() technique can
be used for making a Java property file in XML format.

How to Create and Modify Properties File Form Java Program in Text and XML Format - Example

Java program to create and retailer Properties in textual content and XML format

How to create Property file from Java program text and XML formatAs I stated earlier java.util.Properties symbolize property file in
Java program. It supplies
load() and retailer() technique to
learn and write properties recordsdata from and to the File system. Right here is an easy instance
of making Java property file type program itself.
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * Java program to create and modify property file in textual content format
and storing

 * property
on it. More often than not we use textual content editor to create and edit property

 * file e.g. jdbc.properties
or log4j.properties however we will additionally create property

 * file from
Java program as proven on this instance.

 *
 * @creator Javin Paul
 */

public class
TextPropertyWriter {

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

        //Creating
properties recordsdata from Java program

        Properties props = new Properties();
        FileOutputStream fos = new FileOutputStream(“c:/person.properties”);
     
        props.setProperty(“key1”,
“value1”);
        props.setProperty(“key2”,
“value2”);
     
        //writing properites
into properties file from Java

        props.retailer(fos, “Properties
file generated from Java program”
);
     
        fos.shut();
     
    }
}

This can create person.properties file in C:. right here is how that property
file will appear like:

#Properties file generated from Java program

#Mon Jan 16 03:00:57 VET 2012

key2=value2

key1=value1

Right here is one other instance of making Java property file in XML format from
Java program:

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

/**
 * Java program to retailer properties in XML property file. stroeToXML()
technique of

 * java.util.Properties
class is used to save lots of properties in XML property file from Java

 * program.

 *
 * @creator Javin Paul
 */

public class
XmlPropertiesWriter {

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

        //Studying properties
recordsdata in Java instance

        Properties props = new Properties();
        FileOutputStream fos = new FileOutputStream(“c:/person.xml”);
     
        props.setProperty(“key1”,
“value1”);
        props.setProperty(“key2”,
“value2”);
     
        //writing properties
into properties file from Java

        props.storeToXML(fos, “Properties
file in xml format generated from Java program”
);
     
        fos.shut();

     
    }
}

Output:

<?xml model=“1.0” encoding=“UTF-8” standalone=“no”?>
<!DOCTYPE properties SYSTEM
“http://java.solar.com/dtd/properties.dtd”>

<properties>
<remark>Properties file
in xml format generated from Java program</remark>
<entry
key=“key2”>value2</entry>
<entry
key=“key1”>value1</entry>
</properties>



That’s all on Easy methods to create a Properties file from the Java program or Easy methods to
modify Properties from the Java program. As I stated nearly on a regular basis we use textual content
editor e.g. notepad or notepad++ to edit properties recordsdata like
log4j.properties or jdbc.properties, you’ll be able to
additionally edit them from the Java program if wanted. 

I personally favor properties
file in textual content format if the variety of properties just isn’t a lot and XML-based
properties file if the file is large enough like if in case you have many properties to
leverage XML editors.

Different Java XML tutorials you could discover helpful



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments