• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

read property file and store it in hashMap

 
Ranch Hand
Posts: 493
Android Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
S Shehab
Ranch Hand
Posts: 493
Android Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi ,
the key will be what in "Agents" & the value what in "Agents Ips "..
 
Prasad Tamirisa
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I notice that there are two more IPs than agents in your example. How are you supposed to handle that situation?
 
S Shehab
Ranch Hand
Posts: 493
Android Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic