Friday, April 26, 2024
HomeJavaThe way to learn Properties File in Java – XML and Textual...

The way to learn Properties File in Java – XML and Textual content Instance Tutorial


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

Studying and writing properties file in Java is just a little totally different than studying or writing textual content file in Java or studying xml information in Java utilizing xml parsers like DOM as a result of Java offers the particular provision to the properties file. For individuals who will not be conversant in Properties information in java, It’s used to characterize configuration values like JDBC connectivity parameter or person desire settings and has been a main supply of injecting configuration on Java functions.  Properties file in Java is a textual content file that shops knowledge within the type of key and worth, key being often called property. Java lets you learn the worth of the property by offering its title which makes it straightforward to entry configuration settings. Many utility like Spring and Spring Boot additionally makes use of property file for configurations for instance utility.properties is often used to setup Spring boot. 

Two extra standard examples of properties file are jdbc.properties usually used to retailer database connection settings like URL, username, and password, and log4j.properties file which settings for java logging utilizing log4j. 


There are additionally many frameworks that use Java properties information like
Struts, Spring, Displaytag, and many others. One other benefit of utilizing properties information is you could characterize knowledge both in xml format or in properties format. xml format can be notably helpful in case you are sharing configuration or settings with some exterior software that understands solely XML. 


On this article, we are going to see find out how to learn and write into Properties information in Java in each xml and textual content format.

And, In case you are new to Java Programming then I additionally suggest you undergo these Java on-line programs on Udemy to study Java in a greater and extra structured approach. This is likely one of the greatest and up-to-date programs to study Java on-line.

How to read Properties File in Java – XML and Text Example Tutorial

Studying values from Java Properties File

example of How to read Properties File in Java – XML and Text formatHere’s a pattern code instance of studying a properties file in Java. On this instance, we are going to learn a property from jdbc.properties file which comprises database connection settings, you might need already used that.
Anyway, it comprises username, password, and jdbc driver class title as a key-value format. We are going to learn these values from the Java program utilizing java.util.Properties class which represents properties file in Java program.

jdbc.username=take a look at
jdbc.password=unknown
jdbc.driver=com.mysql.jdbc.Driver


Code Examples of Studying Properties File in Java

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

public class PropertyFileReader {

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

        //Studying properties file in Java instance
        Properties props = new Properties();
        FileInputStream fis = new FileInputStream(“c:/jdbc.properties”);
     
        //loading properties from a property file
        props.load(fis);

        //studying property
        String username = props.getProperty(“jdbc.username”);
        String driver = props.getProperty(“jdbc.driver”);
        System.out.println(“jdbc.username: “ + username);
        System.out.println(“jdbc.driver: “ + driver);

    }
}

Output:
jdbc.username: take a look at
jdbc.driver: com.mysql.jdbc.Driver

In case you learn any property which isn’t specified within the properties file then you’ll props.getProperty() will return null

Now let’s have a look at one other instance of studying property information from xml format. as you realize properties file in java could be represented in xml format and Java offers a handy methodology known as loadFromXML() to load properties from xml file. here’s a fast instance of parsing xml properties file and studying knowledge.

The way to Learn Property file in XML format – Java

Within the earlier part, we’ve got seen a pattern working code instance of studying properties file (.properties) however as I mentioned earlier you can even outline property in xml format this methodology can also be very fashionable amongst numerous Java logging frameworks e.g. log4j, and likewise amongst others. 

On this part, we are going to see find out how to learn property file which is written in xml format. In case you see the code not many modifications as an alternative of  Properties. load() we’re utilizing Properties.loadFromXML() after which the remainder of the stuff of getting property and printing its worth is similar as within the final instance.

By the way in which right here is our pattern java properties file in xml format, which outlined two entries with key jdbc.username and jdbc.password.

<?xml model=“1.0” encoding=“UTF-8” standalone=“no”?>
<!DOCTYPE properties SYSTEM “http://java.solar.com/dtd/properties.dtd”>
   <properties> <entry key=“jdbc.username”>root</entry>
        <entry key=“jdbc.password”>mysql</entry>
   </properties>

And here’s a Java program that can learn XML properties file in Java:

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

        //Studying properties file in Java instance
        Properties props = new Properties();
        FileInputStream fis = new FileInputStream(“c:/properties.xml”);
     
        //loading properties from properties file
        props.loadFromXML(fis);

        //studying property
        String username = props.getProperty(“jdbc.username”);
        System.out.println(“jdbc.username: “ + username);
     
}

output:
jdbc.username: root

We’ve got seen find out how to learn properties information in java, In each textual content and xml format. Properties file are immensely useful for offering settings and configuration knowledge to any Java program. Textual content properties file can solely characterize linear values however xml properties file can even characterize hierarchical values which makes Properties file most popular alternative in logging frameworks.

Different Java tutorials you could like



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments