| Author |
read property file and store it in hashMap
|
Sherif Shehab
Ranch Hand
Joined: Mar 05, 2007
Posts: 472
|
|
Hello pals, I need to read property file and store it in hashMap, any ideas? Here is an example for my property file : AGENTS = AGENT109 AGENT145 AGENT147 AGENT148 AGENT149 AGENT150 AGENT151 AGENT152 AGENT153 AGENT154 AGENT156 AGENT157 AGENT158 AGENTS IPS = 10.150.15.149 10.150.15.118 10.150.15.126 10.150.15.244 10.150.15.106 10.150.15.250 10.150.15.120 10.150.15.122 10.150.15.119 10.150.15.121 10.150.15.223 10.150.15.212 10.150.15.109 10.150.15.186 10.150.15.156
|
Thanks,
Sherif
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32663
|
|
|
You have lines which correspond to each other in pairs? You can simply put the two into a Map. Go through the Java Tutorials about Collections, and the Map interface in the API, and it is quite easy. Look for the put method.
|
 |
Prasad Tamirisa
Ranch Hand
Joined: Mar 26, 2007
Posts: 130
|
|
How do you want to store it in the hashmap..?? I mean, what is the key which you want to use while retrieving the data back from hash map.??? [ May 17, 2008: Message edited by: Prasad Tamirisa ]
|
Regards,
Durga Prasad
|
 |
Sherif Shehab
Ranch Hand
Joined: Mar 05, 2007
Posts: 472
|
|
hi , the key will be what in "Agents" & the value what in "Agents Ips "..
|
 |
Prasad Tamirisa
Ranch Hand
Joined: Mar 26, 2007
Posts: 130
|
|
Here is your algorithm, 1)Create a class(call it propObject) with the member variable's key (string) and values[] (array of strings). 2)Scan through the complete document and find out the number of "="'s. (say X. 3)Create an array of propObject objects of size X. 4)Scan through the file line by line and if = symbol is encountered store the preceding string as key and all the other values as values[] until you encounter the next = symbol 5)Follow the same for all the objects. 6)Create a hash hap and put the key as key and the corresponding value as values[]. [ May 17, 2008: Message edited by: Prasad Tamirisa ]
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32663
|
|
Not convinced that will work correctly. You will end up with the list of agent numbers as one String, then the list of IPs as a second String. I thought those were separate files, in which case it would be easy: read a line from Agents, use that as Key, and read a line from IPs and use that as Value. You could read all the agents into a StringBuilder, separated by whitespace, then read all the IPs into a similar StringBuilder, get the toString() strings and separate them into String[] arrays with split(). They you would have to iterate through the arrays with a for loop and put the Key and Value into your properties Map. Then serialise your Map for future reference!
|
 |
Prasad Tamirisa
Ranch Hand
Joined: Mar 26, 2007
Posts: 130
|
|
How about this..??
You will end up with the list of agent numbers as one String, then the list of IPs as a second String.
1)Create a class(call it propObject) with the member variable's key (string) and values[] (array of strings). 2)Create a hash hap(call it propMap). 3)Scan through the file line by line and if = symbol is encountered store the preceding string as key. 4)Continue scanning the remaining lines and store the string in each line in to the corresponding values[] until you encounter the next = symbol 5)If you encounter the next = symbol, stop right there and do the following step. 6)Put(Key,values[]) in to the propMap. 7)Clear the values of key and values[]. 8)Repeat the above 3 steps until you reach the end of the file. v Hope this convinces Campbell. Your comments are always welcome. [ May 17, 2008: Message edited by: Prasad Tamirisa ]
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32663
|
|
The problem is that what intervenes between = symbols is not a key, but several keys. so you end up with a Key[] and a Value[]. At least that happens if there is one file in the format above, and it is awkward to handle. If there are (as I first thought) two files, then putting successive values in the Map is really easy.
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
|
I notice that there are two more IPs than agents in your example. How are you supposed to handle that situation?
|
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
|
 |
Sherif Shehab
Ranch Hand
Joined: Mar 05, 2007
Posts: 472
|
|
Hi pals, I think i figrued it , i put the Agents and Ips in separate files then read them in 2 vectors then fill the hashmap with these vectors , i know it's a dumb solution but really i'm in hurry to finish this as soon as possible. Thanks Much for the help
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32663
|
|
|
You're welcome, and well done. But you don't need the two Vectors. You can read one line from file "Agent" and use it as the key, and you use the line from file "IPs" as the value. Really easy.
|
 |
 |
|
|
subject: read property file and store it in hashMap
|
|
|