Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

To read an ini file

 
Uma Viswanathan
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could someone help me to read a configuration file (i.e., ini file) which have section names and then entries
Thanks...
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just read in the file one line at a time and check for key words that you know will be in the ini file. So I guess there is more to this question than I am seeing.
 
Uma Viswanathan
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Cindy Glass,
Thanks for your reply.
I want to parse an ini file like
IPAddress = "199...."
Port = "1000"
under the section say, Server1
The ini file also contains the following
IpAddress = "199..."
Port = "2000"
under the section say, Server2
I could write a program to parse a file if it contains entries under no sections. Hope that i am clear to express my doubt. Could you help me to write a program according to this requirement?
Thanks a lot...
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you use a properties file. The API has support for that without having to code extra.
a.properties
content
label=some value here
Code
String value = System.getProperty("label");
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Checkout the java.util.Properties class. Sounds like this does alot of the work for you.
 
Uma Viswanathan
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul Stevens and christopher foran.
I had already looked into Properties class but i don't how it would be useful to parse an ini file which contains entries "under sections".
I am able to use the Properties class if there is no section and if i have entries directly (i.e, key/value pairs like
Port = "2000" ). If i have entries say Port = "2000" under sections Server1 and Server2, then how should i parse such a file?
Thanks a lot...
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would need to change your key value. Your right it would not recognize a difference in port= under 2 different sections. You would need to say server1port= and server2port=. The alternative is code it all yourself.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic