53

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 ?

Moshe Katz
  • 15,992
  • 7
  • 69
  • 116
devsda
  • 4,112
  • 9
  • 50
  • 87

3 Answers3

30

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  
superpupervlad
  • 122
  • 1
  • 8
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

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
13

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

prasanth
  • 3,502
  • 4
  • 28
  • 42
  • 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 Apr 30 '13 at 08:53
  • As my project name is `WebServer`, m, where can I put log , then ? – devsda Apr 30 '13 at 09:36
  • It is enough that you create a folder named `logs` in your project directory and put an entry in your conf file as `ROOT_DIR=logs`. – prasanth Apr 30 '13 at 10:47
  • It works fine to set log file location, but I cannot solve problem to set location of server.properties. How can I set this in my java code ? – devsda Apr 30 '13 at 11:04
  • Right now I am using these statements, `File f = new File("C://Users//jhamb//Desktop//WebServer//src//main//resources//server.properties"); InputStream is = new BufferedInputStream(new FileInputStream(f)); props.load(is);` How can I change the server.properties path in this , so that there is no need to set always in different machines. – devsda Apr 30 '13 at 11:06
  • Yes, my code is in src-->main-->java-->XXX-->YYY-->ZZZ-->[MY CODE IS HERE] – devsda Apr 30 '13 at 11:11
  • Assuming that you are running your code from src, `File file = new File("main"+File.separator+"resources"+File.separator"+"server.properties)` – prasanth Apr 30 '13 at 11:19
  • Why dont you keep your resources out of src?? In that case you can directly access resources/server.properties. – prasanth Apr 30 '13 at 11:25
  • I didn't understand, Maven has its own File system, and I think all the resources must be put in the resources, conventionally. – devsda Apr 30 '13 at 11:27
  • DO you know any method , just like `log4j` has to load log properties, just `// Configure Log4J PropertyConfigurator.configure(Main.class.getClassLoader() .getResource("log4j.properties"));` – devsda Apr 30 '13 at 11:28
  • 1
    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 Apr 30 '13 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 May 11 '18 at 14:46
  • This answer is incorrect - passing the filename to `Properties.load` won't work - it needs an `InputStream` or a `Reader`, not a `String`. https://docs.oracle.com/javase/7/docs/api/java/util/Properties.html – andydavies Jul 26 '18 at 11:35