How to use java properties file?

For configuration purposes, using properties file is a good way of reusing. In this way, when the code is packaged to a jar file, other users can just put the different configurations in the file. The following is a simple example of using properties file.

1. create the file hierarchy like the following. Mainly remember to put the file under src package. Other testing code and database class are put in different package under src.


2. The following is the code.

package Test;
import java.util.Properties;
public class Test {
	public static void main(String[] args) {
		Properties configFile = new Properties();
		try {
			String name = configFile.getProperty("name");
		} catch (IOException e) {

3. The content in the configuration file following the format of "key=value".

Category >> Real Methods  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
String foo = "bar";
  • seonuke

    Νice answer back іn return of this queгy wіth геal aгguments аnd exρlaining all
    about that.

  • JP @ garbage collection java

    nice tips. I also like you article on java garbage collection , excellent blog mate. keep it up.