Link to home
Create AccountLog in
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.
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
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



Avatar of newone2011
newone2011

ASKER

Ok, let me check
I do not want to this in my code dir, can I specify where I want this property file to be read from ?
>>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
>>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


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

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
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
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

Ok, thanks. Checking
:)