I am doing a project to build thread pooled web server
, in which I have to set
One way is to hard code all these variables in the code, that I did. But professionally it is not good.
Now, I want to make one configuration file, in which I put all these data, and at the run time my code fetches these.
How can I make configuration file for the above task ?
Class ConfigReaderThis class is used to filter configuration files prior to parsing. All configuration files (config. xml and external references) are read using this reader.
. properties is a file extension for files mainly used in Java related technologies to store the configurable parameters of an application. They can also be used for storing strings for Internationalization and localization; these are known as Property Resource Bundles.
app.config
app.name=Properties Sample Code app.version=1.09
Source code:
Properties prop = new Properties(); String fileName = "app.config"; try (FileInputStream fis = new FileInputStream(fileName)) { prop.load(fis); } catch (FileNotFoundException ex) { ... // FileNotFoundException catch is optional and can be collapsed } catch (IOException ex) { ... } System.out.println(prop.getProperty("app.name")); System.out.println(prop.getProperty("app.version"));
Output:
Properties Sample Code 1.09
Create a configuration file and put your entries there.
SERVER_PORT=10000 THREAD_POOL_COUNT=3 ROOT_DIR=/home/
You can load this file using Properties.load(fileName)
and retrieved values you get(key)
;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With