Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please Help!!

 
April Benzine
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic