Quantcast
Channel: Tech Tutorials
Viewing all articles
Browse latest Browse all 885

How to read Properties file in Java

$
0
0

If you have any configurable data in your application like DB configuration, user settings its better to keep it in a properties file. A properties store data in a form of key/value pair.

In this tutorial you will see how to read a properties file.

Project structure

For this example we’ll have a properties file named app.properties file in a folder called resource. The resource folder is at the same level at the src folder in the Java project.

Steps

  1. Create an instance of Properties class.
  2. Read the properties file.
  3. Load the file to the instance of Properties class using the load method.

Content of the properties file

Here the properties file used is named app.properties file with it’s content as -


user=TestUser
url=https://netjs.blogspot.com

Loading properties file from the file system

One way to read properties file is to load it from the file system.

Example code


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

public class PropDemo {
private Properties appProp = new Properties();
public static void main(String[] args) {
PropDemo pDemo = new PropDemo();
pDemo.loadPropertiesFile();
pDemo.readProperties();


}

// This method is used to load the properties file
private void loadPropertiesFile(){
InputStream iStream = null;
try {
// Loading properties file from the path (relative path given here)
iStream = new FileInputStream("resource/app.properties");
appProp.load(iStream);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if(iStream != null){
iStream.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

/**
* Method to read the properties from a
* loaded property file
*/
private void readProperties(){
System.out.println("User name - " + appProp.getProperty("user"));
System.out.println("URL - " + appProp.getProperty("url"));
// reading property which is not there
System.out.println("City - " + appProp.getProperty("city"));

}

}

Output


User name - TestUser
URL - https://netjs.blogspot.coms
City - null

Here you can see that in the code there is an attempt to read the property “city” which doesn’t exist in the app.properties file that’s why it is retrieved as null.

Loading properties file from classpath

If you have properties file in the project classpath then you can load it by using the getResourceAsStream method.


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

public class PropDemo {
private Properties appProp = new Properties();
public static void main(String[] args) {
PropDemo pDemo = new PropDemo();
pDemo.loadProperties();
pDemo.readProperties();
}

// This method is used to load the properties file
private void loadProperties(){
InputStream iStream = null;
try {
// Loading properties file from the classpath
iStream = this.getClass().getClassLoader().
getResourceAsStream("app.properties");
if(iStream == null){
throw new IOException("File not found");
}
appProp.load(iStream);
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(iStream != null){
iStream.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

/**
* Method to read the properties from a
* loaded property file
*/
private void readProperties(){
System.out.println("User name - " + appProp.getProperty("user"));
System.out.println("URL - " + appProp.getProperty("url"));
}
}

Output


User name - TestUser
URL - https://netjs.blogspot.com

That's all for this topic How to read Properties file in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. How to read file from the last line in Java
  2. Unzipping files in Java
  3. Reading file in Java 8
  4. Writing file in Java
  5. How to read properties file in Spring Framework

You may also like -

>>>Go to Java Programs page


Viewing all articles
Browse latest Browse all 885

Trending Articles