How To Read Properties File Inwards Coffee – Xml In Addition To Text Event Tutorial
Monday, March 4, 2019
Add Comment
Reading together with writing properties file inwards Java is niggling dissimilar 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 amongst Properties file inwards java, It is used to stand upward for configuration values similar JDBC connectivity parameter or user preferences settings together with 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 cardinal together with value, cardinal beingness known every bit property. Java allows you lot to read value of holding past times providing its cite which makes it slowly to access configuration settings. Two pop event of properties file are jdbc.properties frequently used to shop database connecter settings similar URL, username together with password together with 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 wages of using properties file is that you lot tin forcefulness out stand upward for data either inwards xml format or inwards properties format. xml format volition live on specially useful if you lot are sharing configuration or settings amongst to a greater extent than or less external tool which understands precisely xml. In this article nosotros volition see how to read together with write into Properties file inwards Java on both xml together with text format.
Further Learning
Java In-Depth: Become a Complete Java Engineer!
Master Java Web Services together with REST API amongst Spring Boot
How to read XML file inwards Java using SAX parser
Reading values from Java Properties File
database connecter settings , you lot mightiness convey already used that. Anyway it contains username, password together with jdbc driver flat cite every bit cardinal value format. We volition read these values from Java program using java.util.Properties flat which stand upward for properties file inwards Java program.
jdbc.username=test
jdbc.password=unknown
jdbc.driver=com.mysql.jdbc.Driver
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
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 whatever holding which is non specified inwards properties file than you lot volition props.getProperty() volition render null. Now let's encounter another event of reading holding file from xml format. every bit you lot know properties file inwards coffee tin forcefulness out live on represented inwards xml format together with Java provides convenient method called loadFromXML() to charge properties from xml file. hither is a quick event of parsing xml properties file together with reading data:
How to read Property file inwards XML format – Java
In before department nosotros convey seen sample working code event of reading properties file (.properties) but every bit I said before you lot tin forcefulness out also define holding inwards xml format this method is also really pop amid diverse Java logging framework e.g. log4j together with and also amid others. In this department nosotros volition encounter how to read holding file which is written inwards xml format. If you lot encounter the code non many changes instead of Properties.load() nosotros are using Properties.loadFromXML() together with than remainder of materials of getting holding together with printing its value is same every bit inwards final example.
By the means hither is our sample coffee properties file inwards xml format, which defined 2 entries amongst cardinal jdbc.username together with 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>
<!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 programme 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
//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 convey seen how to read properties file inwards java, In both text together with xml format. Properties file are immensely helpful for providing settings together with configuration information to whatever Java program. Text properties file tin forcefulness out precisely stand upward for linear values but xml properties file tin forcefulness out also stand upward for 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 together with REST API amongst Spring Boot
How to read XML file inwards Java using SAX parser
0 Response to "How To Read Properties File Inwards Coffee – Xml In Addition To Text Event Tutorial"
Post a Comment