?
Solved

Reading values from file

Posted on 2003-03-26
10
Medium Priority
?
246 Views
Last Modified: 2010-03-31
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

0
Comment
Question by:jMember
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
10 Comments
 

Expert Comment

by:yohan1701
ID: 8211620
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
 
LVL 2

Accepted Solution

by:
amit_chauhan earned 200 total points
ID: 8212546
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
 

Author Comment

by:jMember
ID: 8213007
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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 

Author Comment

by:jMember
ID: 8213665
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
 
LVL 2

Expert Comment

by:amit_chauhan
ID: 8214148
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
 

Author Comment

by:jMember
ID: 8238681
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
 

Expert Comment

by:yohan1701
ID: 8238969
InputStream is = new FileInputStream( fileName ); //filename is full path or relative path to file
0
 
LVL 2

Expert Comment

by:amit_chauhan
ID: 8240171
I think yohan1701 answered your question.

Thanks
Amit
0
 

Author Comment

by:jMember
ID: 8248355
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
 

Expert Comment

by:yohan1701
ID: 8248402
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

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month9 days, 23 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question