aspose file tools*
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 Java 8 in Action this week in the Java 8 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.
 
jQuery in Action, 2nd edition
 
subject: To read an ini file
 
Similar Threads
including ini file in project
Dealing with Ini Files
hard coding username password
how to read .ini file from java code
How to reload an ini file without restarting weblogic 7.0 ??