How to Read Config File in Java – With Actual Class Example rovided
In my last post I mentioned “Config Class”couple of times.. here you will see actual class and its usage. The class is very simple. We just need to provide proper path to config file we going to use.
The Config File Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.*; import java.util.Properties; public class Config { Properties configFile; public Config() { configFile = new java.util.Properties(); try { configFile.load(this.getClass().getClassLoader(). getResourceAsStream("myapp/config.cfg")); }catch(Exception eta){ eta.printStackTrace(); } } public String getProperty(String key) { String value = this.configFile.getProperty(key); return value; } } |
You can get any property/settings from config with method ‘getProperty’
Here is my config sample file
1 2 3 4 5 |
#This is comment mDbUser = myuser mDbHost = myserver mDbPwds = mypwd mDbName = mydb |
Usage:
1 2 |
cfg = new Config(); dbname = cfg.getProperty("mDbUser"); |
I hope you find this useful. If you wish to read more Java articles from our site visit this.
22 Comments
how to read multiple .cfg file
very helpful and clean. Maybe few comment can add more value
usage
Config cfg = new Config();
String dbname = cfg.getProperty(“mDbUser”);
Thanks Adil.
I will try and improve the quality of the articles.
Hey i have a question about the .conf iles in java, could u email me=
hello, i need to develop a component java/j2ee that will serve to compare 2 configurations of two projects and then do the impact.
Someone help me please to identify my classes and do my class Diagram.
And here is the Singleton version:
import java.util.Properties;
public class Config {
private Properties configFile;
private static Config instance;
private Config() {
configFile = new java.util.Properties();
try {
configFile.load(this.getClass().getClassLoader().getResourceAsStream(“config.cfg”));
} catch (Exception eta) {
eta.printStackTrace();
}
}
private String getValue(String key) {
return configFile.getProperty(key);
}
public static String getProperty(String key) {
if (instance == null) instance = new Config();
return instance.getValue(key);
}
}
Usage:
$value = Config.getProperty(key);
Thanks @Jerome
Is it possible to load the config file from a file on the file system?
Like System.getProperty(“user.homer”)+”/myconfig.cfg” ??
Hope for reply, thanks!
Yes, you can load config from a file on the file system. here it is how you can do it
Properties prop = new Properties();
prop.load(new FileInputStream("c://config.properties"));
System.out.println(prop.getProperty("name_of_variable_in_config"));
You can also refer our article Write Config File in Java
Thanks for stopping by. See you around!
Thank you! helped me a lot!
Thanks 😉
Do you know how to user variables on selection step installation ?
I think I did not get your question. Can you elaborate some more please.
Everyone loves what you guys tend to be up too. This type of clever work and coverage!
Keep up the terrific works guys I’ve included you guys to our blogroll.
Hello do you by any change have one of these formats to write to a cfg file in one of the field
cfg = new WriteConfig();
cfg.setProperty(“mDbUser”) = myuser
Not at the moment.
I did not understand how you going to use this functionality, just to prepare properties file? OR you want to load the updated properties file runtime?
It Works, my file path was wrong so i was getting error.
I have past the same code as above, only thing i have changed is the name of the config file but am getting the following error.
java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Unknown Source)
at java.util.Properties.load0(Unknown Source)
at java.util.Properties.load(Unknown Source)
Suppose my code requires to store multiple user name???
I did normally i.e.
db.user=user1
db.user=user2
.
.
.
.
but it is extracting the last user only
Plz suggest me some alternative
@mayank99
Properties file is simply a set of key value pair. If you assign multiple values to same key the latest assignment is going to override previous one. You can assign users as comma separated list and then fetch and process it to give you list of users.
e.g.
db.user=user1,user2
Awesome, thanks!
Thanks it helped me a lot !
help full, thanks