roast_soul
roast_soul

Reputation: 3650

How to maintain a configuration file in java application project like a configuration file in C# application?

In C#,when I want to create a configuration file, it's so easy,just right click the mouse and add a new configuration file, this file will be added into the solution and it's so easy to maintain.

But in java, I don't know what method is standard. I see some people use the properites file.If this is the most popular method, can some one tell me where to place this file? I saw some guy put it in the src folder, others put it in an external folder.

Can you tell me which is the standard? And what is the best practice to maintain a configuration.

Upvotes: 2

Views: 2368

Answers (2)

tomgeraghty3
tomgeraghty3

Reputation: 1254

I don't know if this is the "standard" way but I think it's the easiest. If you place your properties file in your project's root folder

            - project
                - config.properties
                - src
                    - main
                    - ...
               - test

When you create a File instance in Java and specify a relative filename, then the name is resolved against the directory that Java was launched from

e.g. if you launch java in your command prompt as follows:

            cd C:\Users\Tom\example-project
            java example-project

and this is your code:

            File file = new File("tom.txt");

then the file variable will be resolved to the abolsute path: C:\Users\Tom\example-project\tom.txt

When you Run a project through Eclipse, Eclipse launches java from the root directory of the project, meaning that if you put your config file in the project's root folder then

          File file = new File("name-of-config-file.properties");

will resolve to the correct config file on your system.

This has an added benefit if you create a runnable JAR, as you can just place your config file in the same directory as your JAR and the code will continue to work (the config file location will be resolved relative to the JAR).

If you put your config file in /src folder then you need to have separate code for when running from Eclipse and when running as a JAR

With regards to sample code:

            //Read properties from disk
            File propertiesFile = new File("config.properties");
            FileReader reader = new FileReader(propertiesFile);
            Properties props = new Properties();
            props.load(reader);

            //Set and get properties
            props.setProperty("NewProperty", "value");
            String propValue = props.getProperty("propToGet");

            //Write properties to disk
            FileWriter writer = new FileWriter(propertiesFile);
            props.store(writer, "Added x properties");

Upvotes: 2

Rohit
Rohit

Reputation: 10236

Configuration files are used to store,read write user settings.

I think for web apps you can use web.xml.And for other you should use Properties class to read and write settings.

As for where to place it,If you dont specify path it is stored in your root folder other than that you have to provide explicit path.

Upvotes: 0

Related Questions