This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes To read an ini file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "To read an ini file" Watch "To read an ini file" New topic
Author

To read an ini file

Uma Viswanathan
Ranch Hand

Joined: Jun 14, 2001
Posts: 126
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

Joined: Sep 29, 2000
Posts: 8521
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.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Uma Viswanathan
Ranch Hand

Joined: Jun 14, 2001
Posts: 126
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

Joined: May 17, 2001
Posts: 2823
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

Joined: Mar 16, 2001
Posts: 984

Checkout the java.util.Properties class. Sounds like this does alot of the work for you.


Please ignore post, I have no idea what I am talking about.
Uma Viswanathan
Ranch Hand

Joined: Jun 14, 2001
Posts: 126
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

Joined: May 17, 2001
Posts: 2823
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: To read an ini file
 
Similar Threads
Dealing with Ini Files
How to reload an ini file without restarting weblogic 7.0 ??
hard coding username password
how to read .ini file from java code
including ini file in project