Reading Property File using Resource Bundle in Java

In this tutorial, I am attaching simple program about, Reading Property File using Resource Bundle in Java.

ResourceBudleExample.java

package com.dineshkrish.utilities;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
public class ResourceBudleExample {
public static void main(String[] args) {
// File name you can change the file name and location accordingly
String fileName = "app_property.properties";
// Defining File Object
File file = new File(fileName);
FileReader reader = null;
try {
// Defining File Reader object by passing File Object
reader = new FileReader(file);
// Defining ResourceBundle Object
ResourceBundle bundle = new PropertyResourceBundle(reader);
// Printing Resources from Properties file
System.out.println("Name : "+bundle.getString("name"));
System.out.println("Country : "+bundle.getString("country"));
System.out.println("Mobile : "+bundle.getString("mobile"));
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
e.printStackTrace();
} catch (IOException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}

app_property.properties

#Example Property File

name = Dinesh Krishnan
country = India
mobile = +91 9941937705

Output

—————

Name : Dinesh Krishnan
Country : India
Mobile : +91 9941937705

References

1. Java API Documentaion

Tags:

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *