[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

best way to go through a property file

Posted on 2006-05-13
11
Medium Priority
?
209 Views
Last Modified: 2010-03-31
Hi

I want to search through a property file that has data (see below) that I want to grab the values from

username=jack
password=password
username=tim
password=iamgreat
username=alex
password=dummy

I would like to grab the actual usrname and password pairing and place them in another property file

Would this work or is there a better way?  Don't need to sort them at all just grab the actual username and password and place them in a property file

I also dont know exactly know what is in the property file so I need to check with two strings (username and password)

String search1="username";
String search2="password";
String notfound="notfound";
Properties newprop=null;
 for(Enumeration e = config.propertyNames(); e.hasMoreElements();)
          {
              String propertyName1 = (String) e.nextElement();
              String propertyValue1 = config.getProperty(propertyName);
              String propertyName2 = (String) e.nextElement();
              String propertyValue2 = config.getProperty(propertyName);
                       
                        //some property file have username  and phonenumber and not username and password
                        if ((propertyName1.equals(search1) && (propertyName2.equals(search2))
                           {
                             newprop.store(propertyValue1,propertyValue2);
                           }
                       else
                          newprop.store(notfound,notfound);
                          break;

          }

return newprop;


thanks
ryno71
0
Comment
Question by:ryno71
  • 6
  • 4
11 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 16675415
Why don't you just do

String username = props.getProperty)"username");

?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16675419
Typo there of course

String username = props.getProperty("username");

Load it first with

Properties props = new Properties();
props.load(new FileInputStream("x.properties"));
0
 

Author Comment

by:ryno71
ID: 16675470
you mean somethign like this?

String search1="username";
String search2="password";
String notfound="notfound";
String search1,search2;
Properties props = new Properties();
props.load(new FileInputStream("x.properties"));

String search1 = props.getProperty(search1);
String search2 = props.getProperty(search2);

while(!props.isEmpty())
{
          search1 = props.getProperty(search1);
          search2 = props.getProperty(search2);

         if (search1.equals(null))&&(search2.equals(null))
      {
            newprop.store(notfound,notfound);
break;
      }
        else
            {
            newprop.store(search1, search2);
             }

}

or would you still use


Enumeration e = props.propertyNames();
              while (e.hasMoreElements()){
              search1 = props.getProperty(search1);
              search2 = props.getProperty(search2);                  
}

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 86

Expert Comment

by:CEHJ
ID: 16675475
You don't need to enumerate or loop anything. If there's a property called 'username' then getProperty will get it
0
 

Author Comment

by:ryno71
ID: 16675487
I would need to loop if I wanted all of the usernames and passwords though...  just not one
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16675497
There is only one. Properties contains *unique* names (unless the file was generated through Properties)
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 1400 total points
ID: 16675501
Sorry

was=was not
0
 

Author Comment

by:ryno71
ID: 16675515
I am being passed a property file like below where the person does a store type of thing (Propertie)

So if I was given the property file below

username=jack
password=password
username=tim
password=iamgreat
username=alex
password=dummy

unless I wanted to get jack and password I would have to enumerate.  

thanks
ryno71
0
 

Author Comment

by:ryno71
ID: 16675569
Thanks CEHJ.

didn't see the other post.  That scenario wouldn't happen if they were using Properties...

Thanks
0
 
LVL 16

Expert Comment

by:Peter Kwan
ID: 16675580
The only way is to
1. use Reader to read each line (suppose one property per line).
2. for each line, split the line with the delimiter "=".
3. Store the two values (username and password) into a map, if the key is "password".

A sample code is as below:
BufferedReader br = new BufferedReader(new FileReader("x.properties"));
String line = null;
Map m = new HashMap();
String username = "";
String password = "";
while ((line = br.readLine()) != null) {
     String[] keyVal = line.split("=");
     if (keyVal[0].equals("password")) {
          password = keyVal[1];
          m.put(username, password);
      }
    else {
         username = keyVal[1];
     }
}

Then you can use enumeration to loop through the map.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16677002
:-)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month19 days, 11 hours left to enroll

872 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