How to Read/Write Java Properties Files

Java properties files are just simple text files that are widely used in Java to store different properties which should not be hard coded in java source files. By convention, properties files should have the .properties extension. They have a simple structure, each line defining a key/value pair. It is also possible to define larger text values on multiple lines. The main benefit of the properties files is the one that they can be easily edited by hand using any text editor.

Java properties files can be stored in

  • in a standard folder, to a known location
  • in classpath (this way it can be packed in a jar file, made is accessible when the application runs)

There are several options to handle the Java properties files:

To read java properties files from the classpath:

The properties files can be stored in the classpath. This way they can be put inside jar files and it’s really useful for web applications when the absolute location of the properties files is not known. When I tested this in an web application it didn’t work:

Properties properties = new Properties() ;
URL url =  ClassLoader.getSystemResource("test.properties");
properties.load(new FileInputStream(new File(url.getFile())));

To read java properties files from the classpath in a web application:

The following example works to load the properties files in a web application. This snippet was tested on Tomcat 5.5. The ‘/’ represents the root of the class path. Otherwise the properties file location is considered relatively to “this” class (or to MyClass for the second example).

Let’s take the first example. Just create a test.properties file right in the the src folder of your application. When you compile the application the file will be copied by the compiler in the classes folder of the web app. This is where the ‘/test.properties’ is loaded from. In a similar manner we can dedicate a folder inside the src folder to all the properties file(in that case we specify the full path like that: “/config/test.properties”).

Properties properties = new Properties() ;
properties.load(this.getClass().getResourceAsStream("/test.properties"));

Similar example to use in a static context:

Properties properties = new Properties() ;
properties.load(MyClass.class.getResourceAsStream("/test.properties"));

Now we came to the second example similar to the first one, when we create the properties file in the src folder in the same location(package) where the specific class is created. The compiler will again take the task to copy the properties file in the classes folder of the webapp. As long as we load it from the same folder with the current class we no longer need to specify an location relative to the “root” folder.

Properties properties = new Properties() ;
properties.load(this.getClass().getResourceAsStream("test.properties"));

To read java properties files from a specific location:

The properties files can be loaded from any location.

Properties properties = new Properties() ;
properties.load(new FileInputStream("C:\\tmp\\test.properties"));

Did you enjoy this tutorial? Be sure to subscribe to the my RSS feed not to miss my new posts!
... or make it popular on

16 Comments

  1. What do you mean by “the root of the class path”? Do you mean the default package location? WEB-INF/src (or WEB-INF/classes)?

  2. yes. I don’t have the src folder in WEB-INF. When the aplication is deployed the java files are compiled in WEB-INF/classes and the resources are simply copied there.

    So I put the properties files directly in the src folder.

  3. Actually is not possible to write properties files in classpath, because the properties are picked up by jvm and they are read as resources. It’s the JVM to control when they are loaded and once loaded they are reloaded. In less words, it’s not possible as I know.

    Of course you can modify the files, but not using them as classpath resources, and you can not use the reading methods I described earlier;l sorry for the misleading title.

    Initially I intended to put all the read/write methods for property files but I ended putting only those I’m using.

  4. I don’t understand this. How do I create a .properties file?
    Its not making sense to me. Thanks in advance,
    Kovid

  5. import java.io.*;
    import java.util.*;

    public class WriteProperty{
    String str, key, val;
    public static void main(String[] args) {
    WriteProperty w = new WriteProperty();
    }
    public WriteProperty(){
    try{
    int check=0;
    while(check == 0){
    check=1;
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    System.out.println(“Enter file name which has properties extension:”);
    str = bf.readLine();
    Properties pro = new Properties();
    File f = new File(str + “.properties”);
    if(!f.exists()){
    check=0;
    System.out.println(“File not found!”);
    }
    else{
    FileInputStream in = new FileInputStream(f);
    pro.load(in);
    System.out.print(“Enter Key : “);
    key = bf.readLine();
    System.out.print(“Enter Value : “);
    val = bf.readLine();
    pro.setProperty(key, val);
    pro.store(new FileOutputStream(str + “.properties”),null);
    System.out.println(“Operation completly successfuly!”);
    }
    }
    }
    catch(IOException e){
    System.out.println(e.getMessage());
    }
    }
    }

  6. After I load the properties from the classpath I’m getting the BOM characters  appended to the start – meaning when I try and use getProperty() for my first property I get null returned – any ideas what I’m doing wrong here??

  7. Hello.
    I just want to say thanks. This post really helped me.
    I used this form to resolve my problem:
    “Properties properties = new Properties() ;
    properties.load(new FileInputStream(“C:\\tmp\\test.properties”));”

    Again, thank you!

Leave a Comment.