This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to read properties file?

 
Kousik Majumder
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mintoo kumar
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
do you want to read the properties file ?
_____________
Mintoo
SCJP 1.4
___________
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rajkamal Pillai
Ranch Hand
Posts: 445
1
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Raj Kamal R",
Please check your private messages regarding an important administrative matter.
-Ben
 
Binod Suman
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi binod, Use code tag to post your code. so that we can read your code easily
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic