Updates
  • Starting New Weekday Batch for Java Full Stack Developer on 24th June 2024 @ 02:00 PM to 04:00 PM
  • Starting New Weekend Batch for Java Full Stack Developer on 06th July 2024 @ 11:00 AM to 02:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 08th July 2024 @ 04:00 PM to 06:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 29th July 2024 @ 10:00 AM to 12:00 PM
Join Course

Properties

The Properties class is available in the java.util package and extends the java.util.Hashtable class.

java.util.Properties:

public class java.util.Properties extends java.util.Hashtable<java.lang.Object, java.lang.Object> {
protectedjava.util.Properties defaults;
publicjava.util.Properties();
publicjava.util.Properties(java.util.Properties);
public synchronized java.lang.ObjectsetProperty(java.lang.String, java.lang.String);
public synchronized void load(java.io.Reader) throws java.io.IOException;
public synchronized void load(java.io.InputStream) throws java.io.IOException;
public void save(java.io.OutputStream, java.lang.String);
public void store(java.io.Writer, java.lang.String) throws java.io.IOException;
public void store(java.io.OutputStream, java.lang.String) throws java.io.IOException;
public synchronized void loadFromXML(java.io.InputStream) throws java.io.IOException, java.util.InvalidPropertiesFormatException;
public void storeToXML(java.io.OutputStream, java.lang.String) throws java.io.IOException;
public void storeToXML(java.io.OutputStream, java.lang.String, java.lang.String) throws java.io.IOException;
publicjava.lang.StringgetProperty(java.lang.String);
publicjava.lang.StringgetProperty(java.lang.String, java.lang.String);
publicjava.util.Enumeration<?>propertyNames();
publicjava.util.SetstringPropertyNames();
public void list(java.io.PrintStream);
public void list(java.io.PrintWriter);
static {};
}

Important point of java.util.Properties class:

1. We use a Properties class object to store elements in key-value format.
2. Both the key and value will be String objects.
3. It doesn't inherit the load-factor functionality from the superclass.
4. In application development, the java.util.Properties class object is commonly used to access .properties files.

Important Constructor of java.util.Properties class:

1. public java.util.Properties(): It creates an empty properties object with no default value.

2. public java.util.Properties(java.util.Properties): It creates an empty properties object with the specified default value.

Important Method of java.util.Properties class:

1. public synchronized java.lang.ObjectsetProperty(java.lang.String, java.lang.String): This method internally calls the put() method of the java.util.Hashtable class, returning the previous value associated with the specified key. If there is no previous mapping, it returns null. It throws a NullPointerException when attempting to map with a null key or null value.

2. public java.lang.StringgetProperty(java.lang.String): Searches for the property with the specified key in this property list. If the key is not found in this property list, the default property list and its defaults are checked recursively. The method returns null if the property is not found. It throws a NullPointerException when a null key is specified.

3. public java.lang.StringgetProperty(java.lang.String, java.lang.String): Searches for the property with the specified key in this property list. If the key is not found in this property list, the default property list and its defaults are checked recursively. The method returns the default value argument if the property is not found.

4. public java.util.EnumerationpropertyNames(): Returns an enumeration of all the keys in this property list, including distinct keys in the default property list if a key of the same name has not already been found in the main properties list.

5. public java.util.Set<java.lang.String>stringPropertyNames(): Returns an unmodifiable set of keys from this property list where both the key and its corresponding value are strings. This set includes distinct keys from the default property list if a key of the same name has not already been found in the main properties list. Properties whose key or value is not of type String are omitted.

            
importjava.util.Enumeration;
importjava.util.Properties;
importjava.util.Set;
publicclass JTC {
   publicstaticvoid main(String[] args) {
      // Creating Properties with no default-value or default-properties using default
      // constructor.
      Properties properties = newProperties();
      // Mapping elements in Key-Value format using setProperty() method.
      System.out.println(properties.setProperty("K-1", "V-1"));
      System.out.println(properties.setProperty("K-2", "V-2"));
      System.out.println(properties.setProperty("K-3", "V-3"));
      System.out.println(properties.setProperty("K-4", "V-4"));
      System.out.println(properties.setProperty("K-5", "V-5"));
      System.out.println("properties :- " + properties);
      System.out.println(properties.setProperty("K-1", "V-2"));
      // System.out.println(properties.setProperty(null, "V-2")); --->
      // NullPointerException
      // System.out.println(properties.setProperty("K-1", null)); --->
      // NullPointerException
      System.out.println("properties :- " + properties);
      // Getting mapping from current working properties on the basis of specified key
      // using getProperty() method.
      System.out.println(properties.getProperty("K-1"));
      System.out.println(properties.getProperty("No-Key"));
      Properties properties2 = newProperties(properties); // Creating a property list with default-property-list.
      properties2.setProperty("K-6", "V-6");
      properties2.setProperty("K-7", "V-7");
      properties2.setProperty("K-8", "V-8");
      properties2.setProperty("K-9", "V-9");
      System.out.println("properties2 :- " + properties2);
      // Getting mapping from current working property which has default property
      // using getProperty() method.
      System.out.println(properties2.getProperty("K-8"));
      System.out.println(properties2.getProperty("K-2"));
      System.out.println(properties2.getProperty("No-key"));
      // System.out.println(properties2.getProperty(null)); --->NullPointerException
      System.out.println(properties2.getProperty("K-1", "DEFAULT_VALUE"));
      System.out.println(properties2.getProperty("No-Key", "DEFAULT_VALUE"));
      // System.out.println(properties2.getProperty(null, "DEFAULT_VALUE")); --->NullPointerException
      System.out.println(properties2.getProperty("No-Key", null));
      // Getting the Enumeration of all the key of the current working property (default-property-key included) using propertyName().
      System.out.println("--------------");
      Enumerationenumeration = properties2.propertyNames();
      while (enumeration.hasMoreElements()) {
         System.out.println(enumeration.nextElement());
      }
      System.out.println("--------------");
      // Getting Unmodifiable Set which contains all the Key of current working property (default-propert-key included) using setringPropertyNames().
      Setset = properties2.stringPropertyNames();
      System.out.println("set :- " + set);
      // set.add("New-Key"); --->UnsupportedOperationException
      // set.remove("K-1"); --->UnsupportedOperationException
   }
}
    null
    null
    null
    null
    null
    properties :- {K-3=V-3, K-2=V-2, K-5=V-5, K-4=V-4, K-1=V-1}
    V-1
    properties :- {K-3=V-3, K-2=V-2, K-5=V-5, K-4=V-4, K-1=V-2}
    V-2
    null
    properties2 :- {K-7=V-7, K-6=V-6, K-9=V-9, K-8=V-8}
    V-8
    V-2
    null
    V-2
    DEFAULT_VALUE
    null
    --------------
    K-9
    K-8
    K-7
    K-6
    K-5
    K-4
    K-3
    K-2
    K-1
    --------------
    set :- [K-3, K-2, K-5, K-4, K-7, K-6, K-9, K-8, K-1]
            

In this example, we have used an important method of the java.util.Properties class to understand its implementation.

Working with Properties file:

Q. What is Properties File?

• A properties file is a file that contains all the configuration of a Java application in a key and value format. It behaves like an external resource for a Java application.
• The extension of a properties file is ".properties.
• When we write the configuration directly into the Java program, making changes to the configuration becomes challenging, as we need to modify the same configuration in multiple places in the code. Additionally, updating the .class file requires recompiling the entire application after every change. Using a properties file helps address these issues.

Important Method ofjava.util.Properties class:

1. public synchronized void load(java.io.Reader) throws java.io.IOException: It reads a property list (key-value pairs) from an input character stream in a simple line-oriented format.

2. public synchronized void load(java.io.InputStream) throws java.io.IOException: It reads a property list (key-value pairs) from the input byte stream. The input stream is in a simple line-oriented format.

            
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.IOException;
importjava.util.Properties;
publicclass JTC {
   publicstaticvoid main(String[] args) throwsFileNotFoundException, IOException {
      Properties properties = newProperties();
      properties.load(new FileInputStream("C:\\Users\\JTC\\eclipse-workspace\\Exception\\src\\com\\P1\\Test.properties"));
      System.out.println(properties.getProperty("UserName"));
      System.out.println(properties.getProperty("Password"));
   }
}
    JTC
    JTC@123
            

3. public void store(java.io.OutputStream, java.lang.String) throws java.io.IOException; The store() method of the java.util.Properties class is used to write the content in Key-Value format or as a comment.

            
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.util.Properties;
publicclass JTC {
   publicstaticvoid main(String[] args) throwsFileNotFoundException, IOException {
      Properties properties = newProperties();
      properties.load(new FileInputStream("C:\\Users\\JTC\\eclipse-workspace\\Exception\\src\\com\\P1\\Test.properties"));
      System.out.println("properties :- " + properties);
      properties.setProperty("K-1", "V-1");
      properties.store(new FileOutputStream("C:\\Users\\JTC\\eclipse-workspace\\Exception\\src\\com\\P1\\Test.properties"), null);
      System.out.println("properties :- " + properties);
   }
}
    properties :- {UserName=JTC, Password=JTC@123}
    properties :- {UserName=JTC, K-1=V-1, Password=JTC@123}
            

Test.properties:

            
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.util.Properties;
publicclass JTC {
   publicstaticvoid main(String[] args) throwsFileNotFoundException, IOException {
      Properties properties = newProperties();
      properties.load(new FileInputStream("C:\\Users\\JTC\\eclipse-workspace\\Exception\\src\\com\\P1\\Test.properties"));
      System.out.println("properties :- " + properties);
      properties.setProperty("K-1", "V-1");
      properties.store(new FileOutputStream("C:\\Users\\JTC\\eclipse-workspace\\Exception\\src\\com\\P1\\Test.properties"), null);
      System.out.println("properties :- " + properties);
      properties.store(new FileOutputStream("C:\\Users\\JTC\\eclipse-workspace\\Exception\\src\\com\\P1\\Test.properties"), "This is Comment");
   }
}

Test.properties:

4. public synchronized void loadFromXML(java.io.InputStream) throws java.io.IOException, java.util.InvalidPropertiesFormatException: Loads all the properties represented by the XML document from the specified input stream into this properties table.

            
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.util.Properties;
publicclass JTC {
   publicstaticvoid main(String[] args) throwsFileNotFoundException, IOException {
      Properties properties = newProperties();
      properties.loadFromXML(new FileInputStream("C:\\Users\\JTC\\eclipse-workspace\\Exception\\src\\com\\P1\\Test.xml"));
      System.out.println("properties :- " + properties);
   }
}

Test.xml:

    properties :- {Username=JTC, K-1=value-1, Password=JTC@123}
            

5. public void storeToXML(java.io.OutputStream, java.lang.String) throws java.io.IOException: It stores the properties into the specified XML file. If a null value is passed as the second argument, the properties are mapped into the XML file without comments. To include a comment, pass the comment message as the second argument.

            
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.util.Properties;
publicclass JTC {
   publicstaticvoid main(String[] args) throwsFileNotFoundException, IOException {
      Properties properties = newProperties();
      properties.setProperty("Username", "JTC");
      properties.setProperty("Password", "JTC@123");
      properties.setProperty("K-1", "V-1");
      // mapping properties in Text.xml with comment [null].
      properties.storeToXML(new FileOutputStream("C:\\Users\\JTC\\eclipse-workspace\\Exception\\src\\com\\P1\\Test.xml"), null);
   }
}

Test.xml: