best way to go through a property file

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
ryno71Asked:
Who is Participating?
 
CEHJCommented:
Sorry

was=was not
0
 
CEHJCommented:
Why don't you just do

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

?
0
 
CEHJCommented:
Typo there of course

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

Load it first with

Properties props = new Properties();
props.load(new FileInputStream("x.properties"));
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ryno71Author Commented:
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
 
CEHJCommented:
You don't need to enumerate or loop anything. If there's a property called 'username' then getProperty will get it
0
 
ryno71Author Commented:
I would need to loop if I wanted all of the usernames and passwords though...  just not one
0
 
CEHJCommented:
There is only one. Properties contains *unique* names (unless the file was generated through Properties)
0
 
ryno71Author Commented:
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
 
ryno71Author Commented:
Thanks CEHJ.

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

Thanks
0
 
Peter KwanAnalyst ProgrammerCommented:
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
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.