Best way to read config info without compiling java code

newone2011
newone2011 used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016
Commented:
Use a Properties file or ResourceBundle, using where possible the format


Server1=222.222.222.190
Awarded 2011
Awarded 2011
Commented:
Awarded 2011
Awarded 2011

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



Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

Author

Commented:
Ok, let me check

Author

Commented:
I do not want to this in my code dir, can I specify where I want this property file to be read from ?
Top Expert 2016

Commented:
>>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
Top Expert 2016

Commented:
>>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
Awarded 2011
Awarded 2011

Commented:


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

Awarded 2011
Awarded 2011

Commented:
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
Awarded 2011
Awarded 2011

Commented:
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
Top Expert 2016

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

Author

Commented:
Ok, thanks. Checking
Top Expert 2016

Commented:
:)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial