Avatar of newone2011
newone2011
 asked on

Best way to read config info without compiling java code

I want to have a config where I can store the server names, any other config related info. What would be the best way/practice to do this so that I do not have store this info in Java file and recompile when it changes.  For example the config file could have the info in following format:

Server1   222.222.222.190
Server2   222.222.222.190

It could have many other rows and the java program should be simply be able reference Server1, server2 and get the latest value. The server1 and its value could be separate by one or more multiple spaces or tabs.
Java

Avatar of undefined
Last Comment
CEHJ

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
CEHJ

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
for_yan

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
for_yan

So in your case you just make file, say MyProperties.properties
with the contents

Server1=222.222.222.190

put it in the deafult directior wher you execute your program
and then after this code:

Properties properties = new Properties();
try {
    properties.load(new FileInputStream("MyProperties.properties"));
} catch (IOException e) {
}

Open in new window


and then you can use the IP address anywhere in your code as

properties.getProperty("Server1");

And you can add any number of properties in the same way



newone2011

ASKER
Ok, let me check
newone2011

ASKER
I do not want to this in my code dir, can I specify where I want this property file to be read from ?
Your help has saved me hundreds of hours of internet surfing.
fblack61
CEHJ

>>properties.load(new FileInputStream("MyProperties.properties"));

Beware of that: it will leave you with an open file

>>
} catch (IOException e) {
}
>>

Beware of that too - it could lead to nasty 'invisible' bugs
CEHJ

>>I do not want to this in my code dir, can I specify where I want this property file to be read from ?

Yes, but it's much more flexible if it's read as a resource on the classpath. It's actually easier to load a ResourceBundle and is less prone to the kind of errors i pointed out above
for_yan



try{
DataInputStream in = new DataInputStream(new FileInputStream("c:\\folder\\MyProperties.properties"));

BufferedReader r = new BufferedReader(in);

 properties.load(r);
}catch(Exception ex){
ex.printStackTrace();
}

}

Open in new window

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
for_yan

In general if you don't specify the folder it would read it not from your code directory, but from
the efault directory where you execute your program;
they maty happen to be the same;
usually they are different

That is true - you never  want to kepp the loop after catch empty, even in the most quick tests,
the best way is to print ex.printStackTrace() after catch()..- then you will be guaranteed that you'll never leave any problems
unnoticed
for_yan

If you plan to distribute your program then ResourceBundle  should be your choice -
if you'll execute it in soem particular place, then reading it from some specified folder is quite OK
CEHJ

Put the folder containing MyProperties.properties on your classpath and do
ResourceBundle rb = ResourceBundle.getBundle("/MyProperties");
	System.out.println(rb.getString("Server1"));

Open in new window

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
newone2011

ASKER
Ok, thanks. Checking
CEHJ

:)