Reading values from Java Properties File

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 in 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 in 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 in 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 read any property which is not specified in properties file than you will props.getProperty() will return null. Now let's see another example of reading property file from xml format. as you know properties file in java can be represented in xml format and Java provides convenient method called loadFromXML() to load properties from xml file. here is a quick example of parsing xml properties file and reading data:
How to read Property file in XML format – Java
In earlier section we have seen sample working code example of reading properties file (.properties) but as I said earlier you can also define property in xml format this method is also very popular among various Java logging framework e.g. log4j and and also among others. In this section we will see how to read property file which is written in xml format. If you see the code not many changes instead of Properties.load() we are using Properties.loadFromXML() and than rest of stuff of getting property and printing its value is same as in last example.
By the way here is our sample java properties file in xml format, which defined two entries with key jdbc.username and 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 here is Java program which will read XML properties file in Java:
public static void main(String args[]) throws FileNotFoundException, IOException {
//Reading properties file in 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 in 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 have seen how to read properties file in java, In both text and xml format. Properties file are immensely helpful for providing settings and configuration data to any Java program. Text properties file can only represent linear values but xml properties file can also represent hierarchical values which makes Properties file preferred choice in logging frameworks.
Other Java tutorial you may like
No comments:
Post a Comment