Using the Properties class

The Properties class is able to contain key/value pairs, just like a Hashtable (in fact, Properties extends from Hashtable). An example of where the Properties class is used is in the System class, that uses this class to store its system properties (like java.version, …). Properties allows its key/value pairs to be loaded and stored and through the methods load and store.

This following example stores the system properties in a file. The store method allows you to save an extra header comment for identification purposes.

Main.java:

import java.util.*;
import java.io.*;
 
public class Main {   
   public static void main(String[] args) throws Exception {
      if (args.length != 1) {
         System.out.println("Usage: java Main <filename>");
         System.exit(1);
      }
 
      BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(args[0]));
      Properties p = System.getProperties();
      p.store(bos, "Our stored System properties");     
   }
}

stores in a specified file:

#Our stored System properties
#Wed Jun 27 17:21:15 CEST 2001
java.specification.name=Java Platform API Specification
awt.toolkit=sun.awt.windows.WToolkit
java.version=1.2.2
java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment
user.timezone=Europe/Paris
java.specification.version=1.2
java.vm.vendor=Sun Microsystems Inc.
java.vm.specification.version=1.0
user.home=C:\WINDOWS
os.arch=x86
java.awt.fonts=
java.vendor.url=http://java.sun.com/
file.encoding.pkg=sun.io
user.region=US
java.home=C:\JDK1.2.2\JRE
java.class.path=c:\jdk1.2.2\jre\lib\rt.jar;C:\PRO
GRA~1\JMF2.1\LIB\SOUND.JAR;C:\PROGRA~1\JMF2.1\LIB\
JMF.JAR;c:\jakarta-log4j-1.0.4\log4j-core.jar;
line.separator=rn
java.ext.dirs=C:\JDK1.2.2\JRE\lib\ext
java.io.tmpdir=c:\windows\TEMP\
os.name=Windows 95
java.vendor=Sun Microsystems Inc.
java.awt.printerjob=sun.awt.windows.WPrinterJob
java.library.path=C:\JDK1.2.2\BIN;.;C:\WINDOWS\SYSTEM;C:\WINDOWS;C:\JIKES\BIN
;C:\ANT\BIN\;C:\JDK1.2.2\BIN;C:\WINDOWS;C:\WIN
DOWS;C:\WINDOWS\COMMAND;;C:\WN16\BIN;C:\PROGRA~1
NETWOR~1\PGP;C:\VIM\VIM55
java.vm.specification.vendor=Sun Microsystems Inc.
sun.io.unicode.encoding=UnicodeLittle
file.encoding=Cp1252
java.specification.vendor=Sun Microsystems Inc.
user.name=esus
user.language=en
java.vendor.url.bug=http://java.sun.com/cgi-bin/bugreport.cgi
java.vm.name=Classic VM
java.vm.specification.name=Java Virtual Machine Specification
java.class.version=46.0
sun.boot.library.path=C:\JDK1.2.2\JRE\bin
os.version=4.10
java.vm.info=build JDK-1.2.2_005, native threads, symcjit
java.vm.version=1.2.2
java.compiler=symcjit
path.separator=;
user.dir=C:\
file.separator=\
sun.boot.class.path=C:\JDK1.2.2\JRE\lib\rt.jar;C:\
JDK1.2.2\JRE\lib\i18n.jar;C:\JDK1.2.2\JRE\classes

You can create your own properties object as follows:

import java.util.*;
import java.io.*;
 
public class Main {   
   public static void main(String[] args) throws Exception {
      // creating a properties object
      Properties p = new Properties();
      p.put("lastlogin", "crazycow");
      p.put("lastpassword", "j23js09zz");
      
      // storing our properties in the file test.prop 
      BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("test.prop"));
      p.store(bos, "Our own properties");     
 
      // loading our properties in another Properties object and 
      // printing its values out.
      Properties p2 = new Properties();
      p2.load(new BufferedInputStream(new FileInputStream("test.prop")));
      p2.list(System.out);
   }
}

Notice that Properties extends from Hashtable, so you can put elements in there that are not Strings. In this case, the load and save methods won’t work properly. A better way of implementating Properties is to have it contain a Hashtable instead of extending from it.