How To Read Properties File Inward Coffee – Xml As Well As Text Event Tutorial

Reading too writing properties file inwards Java is petty unlike than reading or writing text file inwards Java or  reading xml files inwards Java using xml parsers similar DOM because Java provides special provision to properties file. For those who are non familiar with Properties file inwards java, It is used to correspond configuration values similar JDBC connectivity parameter or user preferences settings too has been a primary source of injecting configuration on Java application.  Properties file inwards Java is a text file which stores information inwards shape of primal too value, primal existence known equally property. Java allows you lot to read value of belongings past times providing its get upwards which makes it slow to access configuration settings. Two pop event of properties file are jdbc.properties ofttimes used to shop database connectedness settings similar URL, username too password too log4j.properties file which settings for coffee logging using log4j. There are also many frameworks which uses Java properties file similar Struts, Spring, Displaytag etc. Another payoff of using properties file is that you lot tin dismiss correspond data either inwards xml format or inwards properties format. xml format volition last specially useful if you lot are sharing configuration or settings with approximately external tool which understands simply xml. In this article nosotros volition see how to read too write into Properties file inwards Java on both xml too text format.

Reading values from Java Properties File

Reading too writing properties file inwards Java How to read Properties File inwards Java – XML too Text Example Tutorialdatabase connectedness settings , you lot powerfulness accept already used that. Anyway it contains username, password too jdbc driver shape get upwards equally primal value format. We volition read these values from Java program using java.util.Properties shape which correspond properties file inwards Java program.


jdbc.username=test
jdbc.password=unknown
jdbc.driver=com.mysql.jdbc.Driver


Code Examples of Reading Properties File inwards Java


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

public class PropertyFileReader {

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

        //Reading properties file inwards Java example
        Properties props = new Properties();
        FileInputStream fis = new FileInputStream("c:/jdbc.properties");
     
        //loading properites from properties file
        props.load(fis);

        //reading proeprty
        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: test
jdbc.driver: com.mysql.jdbc.Driver


If you lot read whatsoever belongings which is non specified inwards properties file than you lot volition props.getProperty() volition render null. Now let's meet another event of reading belongings file from xml format. equally you lot know properties file inwards coffee tin dismiss last represented inwards xml format too Java provides convenient method called loadFromXML() to charge properties from xml file. hither is a quick event of parsing xml properties file too reading data:

How to read Property file inwards XML format – Java

In before department nosotros accept seen sample working code event of reading properties file (.properties) but equally I said before you lot tin dismiss also define belongings inwards xml format this method is also rattling pop alongside diverse Java logging framework e.g. log4j too and also alongside others. In this department nosotros volition meet how to read belongings file which is written inwards xml format. If you lot meet the code non many changes instead of  Properties.load() nosotros are using Properties.loadFromXML() too than balance of materials of getting belongings too printing its value is same equally inwards final example.

By the agency hither is our sample coffee properties file inwards xml format, which defined ii entries with primal jdbc.username too jdbc.password.


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

And hither is Java plan which volition read XML properties file inwards Java:

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

        //Reading properties file inwards Java example
        Properties props = new Properties();
        FileInputStream fis = new FileInputStream("c:/properties.xml");
     
        //loading properites from properties file
        props.loadFromXML(fis);

        //reading proeprty
        String username = props.getProperty("jdbc.username");
        System.out.println("jdbc.username: " + username);
     
}

output:
jdbc.username: root


We accept seen how to read properties file inwards java, In both text too xml format. Properties file are immensely helpful for providing settings too configuration information to whatsoever Java program. Text properties file tin dismiss simply correspond linear values but xml properties file tin dismiss also correspond hierarchical values which makes Properties file preferred selection inwards logging frameworks.

Further Learning
Java In-Depth: Become a Complete Java Engineer!
Master Java Web Services too REST API with Spring Boot
How to read XML file inwards Java using SAX parser

0 Response to "How To Read Properties File Inward Coffee – Xml As Well As Text Event Tutorial"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel