File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

How to read properties file?

Kousik Majumder
Ranch Hand

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

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

i.e:- test.properties in path MyApplication/WEB-INF/test.properties

Utils.java in Path MyApplication/WEB-INF/classes/com/test/Utils.java

I want to access the test.properties file from Utils.java.

Can somebody give the code?

Thanks in advance

Kousik


Thanks in Advance,
Kousik
Mintoo kumar
Ranch Hand

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

Joined: Mar 10, 2008
Posts: 1666
    
    7

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: 443
    
    1

Hi,

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


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

Joined: Dec 11, 2004
Posts: 13410

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


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

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 (MyProp.properties and ReadPropFile.java)

2. MyProp.properties
name = Binod Kumar Suman
roll = 110
city = Bangalore

3. ReadPropFile.java

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
import java.util.ResourceBundle;


public class ReadPropFile {

public static void main(String[] args) {
getData();
}

public static void getData(){
try{
Properties propertiesFile = new Properties();
propertiesFile.load(new FileInputStream("src/MyProp.properties"));
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){
e.printStackTrace();
}
}
}


Thanks,

Binod Suman

http://binodjava.blogspot.com/2009/05/how-to-read-properties-file-in-java.html
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: http://aspose.com/file-tools
 
subject: How to read properties file?
 
Similar Threads
Properties file outside the war file
Reading a properties file
accessing a file from a servlet
properties file in websphere
JSP and PropertyResourceBundle