56

I am doing a project to build thread pooled web server, in which I have to set

  • the port number on which server listens.
  • How many threads are there in thread pool
  • Absolute Path of the root directory, and so many points.

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 ?

0

3 Answers 3

33

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  
3
  • 4
    Wher is stored app.config... ? Commented Feb 18, 2020 at 13:27
  • 1
    Next to the executable file, however, you can put it anywhere and enter its full address. Commented Feb 22, 2020 at 6:53
  • 1
    can that file be dynamically used? Such as changing the values from externally
    – showtime
    Commented Apr 5, 2022 at 9:39
14

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

13
  • you can just set value ROOT_DIR=logs/ assuming the project path is your classpath. Your logs will be placed in your project path/ROOT_DIR
    – prasanth
    Commented Apr 30, 2013 at 8:53
  • As my project name is WebServer, m, where can I put log , then ?
    – devsda
    Commented Apr 30, 2013 at 9:36
  • 2
    I solved this, File f = new File(System.getProperty("user.dir")+ File.separator + "src" + File.separator + "main" + File.separator + "resources" + File.separator + "server.properties"); works for me. :) :)
    – devsda
    Commented Apr 30, 2013 at 11:47
  • 1
    Could you provide explain what you understand by fileName in your answer? It's logical to me to put actual String with the name of the file in such variable - but I see I have to use Reader/InputStream here.
    – Line
    Commented May 11, 2018 at 14:46
  • 1
    This answer is incorrect - passing the filename to Properties.load won't work - it needs an InputStream or a Reader, not a String. docs.oracle.com/javase/7/docs/api/java/util/Properties.html
    – andydavies
    Commented Jul 26, 2018 at 11:35
14

It depends.

Start with Basic I/O, take a look at Properties, take a look at Preferences API and maybe even Java API for XML Processing and Java Architecture for XML Binding

And if none of those meet your particular needs, you could even look at using some kind of Database

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.