aspose file tools*
The moose likes Beginning Java and the fly likes Please Help!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Please Help!!" Watch "Please Help!!" New topic
Author

Please Help!!

April Benzine
Greenhorn

Joined: Sep 23, 2004
Posts: 17
I have a directory structure set up. The C rojects\config> directory holds my project1.properties file. then in C rojects\src\java146\util> I have PropertiesManagement.java. This util package will be used in all my projects so I can't hardcode the name of my properties file in here. I need to pass in the properties file name and location useing either the constructor or set methods. Here is my code:

package java146.util;
import java.io.*;
import java.util.*;
/**
* Description of the Class
*
*@author AHobart
*@created September 23, 2004
*/
public class PropertiesManager {
private Properties props;
private String propertiesFileName;

/**
* Constructor for the PropertiesManager object
*/
public PropertiesManager() {

}
/**
* Gets the author attribute of the PropertiesManager object
*
*@return The author value
*/
public String getAuthor() {
return props.getProperty("author");
}
/**
* Gets the email attribute of the PropertiesManager object
*
*@return The email value
*/
public String getEmail() {
return props.getProperty("email");
}
/**
* Gets the course attribute of the PropertiesManager object
*
*@return The course value
*/
public String getCourse() {
return props.getProperty("course");
}
/**
* Gets the section attribute of the PropertiesManager object
*
*@return The section value
*/
public String getSection() {
return props.getProperty("section");
}
/**
* Gets the instructor attribute of the PropertiesManager object
*
*@return The instructor value
*/
public String getInstructor() {
return props.getProperty("instructor");
}
/**
* Gets the day attribute of the PropertiesManager object
*
*@return The day value
*/
public String getDay() {
return props.getProperty("day");
}
/**
* Gets the time attribute of the PropertiesManager object
*
*@return The time value
*/
public String getTime() {
return props.getProperty("time");
}
/**
* Gets the room attribute of the PropertiesManager object
*
*@return The room value
*/
public String getRoom() {
return props.getProperty("room");
}
/**
* Gets the property attribute of the PropertiesManager object
*
*@param key Description of the Parameter
*@return The property value
*/
public String getProperty(String key) {
return props.getProperty(key);
}
/**
* Description of the Method
*/
public void loadProps() {
props = new Properties();
try {
props.load(this.getClass().getResourceAsStream(propertiesFileName));
} catch (IOException ioe) {
System.out.println("Can't load the file: " + ioe);
ioe.printStackTrace();
return;
} catch (Exception e) {
System.out.println("Problem: " + e);
e.printStackTrace();
return;
}
}
}

Please Help me!!
Peter van der Linden
author
Ranch Hand

Joined: Sep 28, 2004
Posts: 46
I need to pass in the properties file name and location useing either the constructor or set methods.


Hi April,
You didn't include a question in your post, but the line above seems to be the focus of
what you are wondering about. Is that right?

Constructors can take parameters just like methods can. You could define it like this:


Then you provide the pathname when you call the constructor:


This is a very common idiom in OOP - passing a value to a constructor which remembers it
in a private variable for later use.

Cheers,

Peter


Author of <a href="http://www.amazon.com/exec/obidos/ASIN/0131482114/ref=jranch-20" target="_blank" rel="nofollow">Just Java(TM) 2 (6th Edition)</a>
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Hi April,

To pass in using Constructor


To pass in using set method


For more info on Properties, you can refer to this link.

Joyce
[ September 28, 2004: Message edited by: Joyce Lee ]
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
Additional solution.

you should be used class for declaration variable of all file use in your project.




And when you call constructor of PropertiesManager with fileproperty parameter.



I think, this solution is more flexible and easy to management your code.


SCJA,SCJP,SCWCD,SCBCD,SCEA I
Java Developer, Thailand
 
 
subject: Please Help!!