Reading values from file

Hello All,

I am fairly new to the java world and i have a easy problem..which goes like this.
I am using a ini file for specifying some web page attributes. For ex. if i have five different pages, (using tables in all the pages)..in each page table, i need to made some columns invisible, but this is dependent on my user type. What i was doing is..i was reading the ini file in all the different pages and then reading the values out of it. But, now i want to make a java class which will serve the same purpose..but, will read the ini file only once at the start.

My ini file structure is like this...

[Page1]
col1 = on
col2 = on
col3 = off
col4 = on
....

[Page2]
col1 = off
col2 = off
col3 = on
col4 = on
....

[Page3]
col1 = on
col2 = on
col3 = off
col4 = off
.....

and so on... on and off means visible and invisible.

My doubts are, how can i make the class static..so that it can be initialised once and used more??
Do i need to have set and get methods for all the columns??

Please guide
thanks and regards
jMember

jMemberAsked:
Who is Participating?
 
amit_chauhanConnect With a Mentor Commented:
Hi,
As Vohan1701 said, java.util.Properties is the perfect class for this purpose. You'll just need to modify your ini file a little for this, as there cannot be duplicate values in the ini file. So your ini file could look something like this:

#[Page1]
page1.col1 = on
page1.col2 = on
page1.col3 = off
page1.col4 = on

#[Page2]
page2.col1 = off
page2.col2 = off
page2.col3 = on
page2.col4 = on

Below is the full code of the class that would load this ini file in memory once, and you could retrieve any property value from it. It implements the singleton pattern as said by vohan1701 to avoid reloading the properties file again:


import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;

/**
 *  Load properties from the properties.ini file
 */
public class PropertiesManager
{

  private static PropertiesManager singleton = null;
  private static Object obj = new Object ();
  private Properties props = null;

  // This is the properties file that will be read. It should be in the classpath
  public static String fileName = "properties.ini";

  /**
   * Make the constructor private so that this class cannot be instantiated by anyone
   */
  private PropertiesManager ()
  {
    init ();
  }

  public static PropertiesManager getInstance ()
  {
    if (singleton == null)
    {
      synchronized (obj)
      {
        if (singleton == null)
          singleton = new PropertiesManager ();
      }
    }
    return singleton;
  }


  /**
   * Load the properties file
   */
  private void init()
  {
    try
    {
      InputStream is = this.getClass().getClassLoader().getResourceAsStream(fileName);

      props = new Properties();

      props.load(is);

    }
    catch(Exception e)
    {
      e.printStackTrace();
    }

  }

  // Gets the value of a given property
  public String getProperty (String propertyName)
  {
    return props.getProperty (propertyName);
  }
}



You can use this class like this :

PropertiesManager pm = PropertiesManager.getInstance ();
String val = pm.getProperty ("page1.col1");


Hope that helps
Thanks
Amit
0
 
yohan1701Commented:
Are you using java.util.Properties ? It will do most of the work of reading the file and retreiving the values.

Any ways to initialise it once use the singleton design pattern.

public class IniLoader {

private IniLoader singleton = null;
private IniLoader() {
//load from here
}

public static IniLoader getInstance() {
if( singleton == null ) {
singleton = new IniLoader();
}
return singleton;
}
//... other class methods here
}
0
 
jMemberAuthor Commented:
Hii Amit and vohan,

Thanks for your kind help. It is the first time that i m using this forum. And i really appreciate both of urs kind help. I don't have enough points with me. Both of u helped me a lot to get the understanding of singleton class.

Thanks and regards
keep up the good work

jMember


0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
jMemberAuthor Commented:
Hii Amit,

Could you please explain me why this synchronize is used here???

synchronized (obj)
     {
       if (singleton == null)
         singleton = new PropertiesManager ();
     }

Thanks and regards
0
 
amit_chauhanCommented:
Sure,
This is to make sure that in no circumstances, should there be 2 instances of the class.
If say 2 threads simultaneously execute the method getInstance (), and at that time singleton is null. Now the first statement,
if (singleton == null)
will succeed for both the threads and they both will enter inside the if clause.
But now I have to make sure that only 1 thread should execute the code of creating an instance of PropertiesManager at 1 time. Thats why the synchronized block. So only 1 thread will enter (any one of the two) the synchronized block and the other will wait till that thread exits the synchronized block. In that block, we check once again whether singleton is null or not and then create an instance of PropertiesManager and assign it to singleton object. After this thread exits the synchronized bloak, the second thread which is waiting will enter. But now, the instance is already created, so, thats why the second check for singleton == null. So in that case, second thread will just by pass the creation of another instance of PropetiesManager.

Hope that explains a bit.
Thanks
Amit
0
 
jMemberAuthor Commented:
Hii Amit,

Thanks for your very good explanation....I have a small question..in case if i don't want the properties.ini file to be in classpath....and want to specify it's own location..Then the following line should be replaced or not???

InputStream is = this.getClass().getClassLoader().getResourceAsStream(fileName);

This case can arise when having some sources but different instances of the same application.

Could you please reply
thanks and regards
0
 
yohan1701Commented:
InputStream is = new FileInputStream( fileName ); //filename is full path or relative path to file
0
 
amit_chauhanCommented:
I think yohan1701 answered your question.

Thanks
Amit
0
 
jMemberAuthor Commented:
Hii yohan and amit,

Thanks for your help. Yohan i created a question for your acceptance a long time back..but, i typed ur name wrong...i typed vohan inspite of yohan...how can i change that???

Can you accept that question??
thanks and regards
keep up the good work
jMember
0
 
yohan1701Commented:
I saw that question
Title: Points for vohan1701 for Q_20563911

I answered with something.
You just need to accept that answer do you ?
0
All Courses

From novice to tech pro — start learning today.