File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes How to read properties file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to read properties file?" Watch "How to read properties file?" New topic

How to read properties file?

Kousik Majumder
Ranch Hand

Joined: Sep 30, 2007
Posts: 237
Hi all,

Can somebody tell me how to get the value of a properties file from a bean class?

i.e:- in path MyApplication/WEB-INF/ in Path MyApplication/WEB-INF/classes/com/test/

I want to access the file from

Can somebody give the code?

Thanks in advance


Thanks in Advance,
Mintoo kumar
Ranch Hand

Joined: Aug 21, 2007
Posts: 61
do you want to read the properties file ?
SCJP 1.4
Jelle Klap

Joined: Mar 10, 2008
Posts: 1950

You could use the ServletContext.getResourceAsStream() method to read the content of the .properties file into a Properties object, using Properties.load() / Properties.loadFromXml(), whichever one is applicable.

Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 445


Not sure if this is what you're looking for:-

[ May 08, 2008: Message edited by: Raj Kamal R ]
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

"Raj Kamal R",
Please check your private messages regarding an important administrative matter.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Binod Suman

Joined: May 07, 2008
Posts: 17

There are many ways to read properties file in java. Here explained two way, using

1. ResourceBundle
2. Properties Class

How to use this tutorial
1. Create one directory src and put both below files ( and

name = Binod Kumar Suman
roll = 110
city = Bangalore


import java.util.Enumeration;
import java.util.Properties;
import java.util.ResourceBundle;

public class ReadPropFile {

public static void main(String[] args) {

public static void getData(){
Properties propertiesFile = new Properties();
propertiesFile.load(new FileInputStream("src/"));
String studentName = propertiesFile.getProperty("name");
String roll = propertiesFile.getProperty("roll");
System.out.println("Student Name :: "+studentName);
System.out.println("Roll Number :: "+roll);

//Fetch all the Properties.

String key;
Enumeration e = propertiesFile.propertyNames();
while (e.hasMoreElements()) {
key = (String)e.nextElement();
System.out.println(key+" "+propertiesFile.getProperty(key));

}catch(IOException e){


Binod Suman
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Hi binod, Use code tag to post your code. so that we can read your code easily
I agree. Here's the link:
subject: How to read properties file?
It's not a secret anymore!