Java.util.Properties class in Java

The Properties class represents a persistent set of properties. The Properties can be saved to a stream or loaded from a stream.

  • Properties is a subclass of Hashtable.
  • It is used to maintain list of value in which the key is a string and the value is also a string.
  • One useful capability of the Properties class is that you can specify a default property that will be returned if no value is associated with a certain key.
  • Multiple thread can share a single properties object without the need of external synchronisation.

Properties defines a instance variable
Properties defaults: This variable holds a default property list associated with a Properties object.
Constructors:

  1. Properties(): This creates a Properties object that has no default values.
  2. Properties(Properties propDefault): The second creates an object that uses propDefault for its default value.

Methods:
In addition to method that Properties inherits from Hashtable, Properties defines some legacy method listed below-

  1. String getProperty(String key): 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, recursively, are then checked. The method returns null if the property is not found.

    Syntax: public String getProperty(String key).
    Returns: the value associated with key.
    Exception: NA.
    
  2. String getProperty(String key, String defaultProperty): 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, recursively, are then checked. The method returns the default value argument if the property is not found.

    Syntax: public String getProperty(String key, String defaultProperty).
    Returns: The value associated with key.
    Exception: NA.
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java code illustrating getProperty() method
    import java.util.*;
    class PropertiesDemo
    {
        public static void main(String arg[])
        {
            Properties gfg = new Properties();
            Set URL;
            String str;
              
            gfg.put("ide", "ide.geeksforgeeks.org");
            gfg.put("contribute", "contribute.geeksforgeeks.org");
            gfg.put("quiz", "quiz.geeksforgeeks.org");
              
            // checking what's in table
            URL = gfg.keySet();
            Iterator itr = URL.iterator();
              
            while(itr.hasNext())
            {
                str = (String)itr.next();
                System.out.println("The URL for " + str + 
                        " is " + gfg.getProperty(str));
            }
              
            System.out.println();
              
            // looking for URL that not in list
            str = gfg.getProperty("articl", "not found");
            System.out.println("The URL for article is " + str);
              
        }
    }

    chevron_right

    
    

    Output:

    The URL for contribute is contribute.geeksforgeeks.org
    The URL for quiz is quiz.geeksforgeeks.org
    The URL for ide is ide.geeksforgeeks.org
    
    The URL for article is not found
    
  3. void list(PrintStream streamOut): Sends the property list to the output stream linked to streamOut.

    Syntax: public void list(PrintStream streamOut).
    Returns: NA.
    Exception: NA.
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java code illustrating list() method
    import java.util.*;
    class PropertiesDemo
    {
        public static void main(String arg[])
        {
            Properties gfg = new Properties();
            Set URL;
            String str;
              
            gfg.put("ide", "ide.geeksforgeeks.org");
            gfg.put("contribute", "contribute.geeksforgeeks.org");
            gfg.put("quiz", "quiz.geeksforgeeks.org");
              
            gfg.list(System.out);
        }
    }

    chevron_right

    
    

    Output:

    -- listing properties --
    contribute=contribute.geeksforgeeks.org
    quiz=quiz.geeksforgeeks.org
    ide=ide.geeksforgeeks.org
    
  4. void list(PrintWriter streamOut): Prints this property list out to the specified output stream. This method is useful for debugging.

    Syntax: public void list(PrintWriter streamOut).
    Return: NA.
    Exception: ClassCastException - if any key in 
    this property list is not a string.
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java code illustrating list() method
    import java.io.PrintWriter;
    import java.util.*;
    class PropertiesDemo
    {
      
          
        public static void main(String arg[])
        {
            Properties gfg = new Properties();
              
            PrintWriter writer = new PrintWriter(System.out);
              
            gfg.put("ide", "ide.geeksforgeeks.org");
            gfg.put("contribute", "contribute.geeksforgeeks.org");
            gfg.put("quiz", "quiz.geeksforgeeks.org");
              
            // printing list using PrintWriter object
            gfg.list(writer);
              
            // flushing the stream
            writer.flush();
        }
    }

    chevron_right

    
    

    Output:

    -- listing properties --
    contribute=contribute.geeksforgeeks.org
    quiz=quiz.geeksforgeeks.org
    ide=ide.geeksforgeeks.org
    
  5. Enumeration propertyNames(): This method returns an enumeration of the keys. This includes those keys found in the default property list, too.

    Syntax: public Enumeration propertyNames().
    Returns: enumeration of key.
    Exception: ClassCastException -- if any key in this 
    property list is not a string.
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java code illustrating propertyNames() method
    import java.util.*;
    class PropertiesDemo
    {
        public static void main(String arg[])
        {
            Properties gfg = new Properties();
            Set str;
            String s;
            gfg.put("ide", "ide.geeksforgeeks.org");
            gfg.put("contribute", "contribute.geeksforgeeks.org");
            gfg.put("quiz", "quiz.geeksforgeeks.org");
              
            Enumeration name = gfg.propertyNames();
              
            // print the enumaration elements
            System.out.println(name.nextElement());
            System.out.println(name.nextElement());
            System.out.println(name.nextElement());
              
        }
    }

    chevron_right

    
    

    Output:

    contribute
    quiz
    ide
    
  6. Object setProperty(String key, String value): Associate value with key. Returns the previous value associated with key, or returns null if no such association exist.

    Syntax: public Object setProperty(String key, String value).
    Returns: the previous value of the specified key in 
    this property list, or null if it did not have one.
    Exception: NA.
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java code illustrating setProperty() method
    import java.util.*;
    class PropertiesDemo
    {
        public static void main(String arg[])
        {
            Properties gfg = new Properties();
              
            gfg.put("ide", "ide.geeksforgeeks.org");
            gfg.put("contribute", "contribute.geeksforgeeks.org");
              
            gfg.setProperty("quiz", "quiz.geeksforgeeks.org");
              
            System.out.println(gfg);
                 
        }
    }

    chevron_right

    
    

    Output:

    {contribute=contribute.geeksforgeeks.org,
     quiz=quiz.geeksforgeeks.org, ide=ide.geeksforgeeks.org}
    
  7. void load(InputStream streamIn): This method reads a property list (key and element pairs) from the input byte stream.

    Syntax: public void load(InputStream streamIn).
    Returns: NA.
    Exception: IOException -- if an error occurred when reading from 
    the input stream.
    IllegalArgumentException -- if the input stream contains a malformed
     Unicode escape sequence.
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java code illustrating load() method
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.*;
    class PropertiesDemo
    {
        public static void main(String arg[]) throws IOException
        {
            Properties gfg = new Properties();
            String s = "ide = ide.geeksforgeeks.org";
              
            FileOutputStream out = new FileOutputStream("properties.txt");
            FileInputStream in = new FileInputStream("properties.txt");  
              
            // write the property in the output stream file
            out.write(s.getBytes());
              
            // load from input stream
            gfg.load(in);
              
            gfg.list(System.out);
        }
    }

    chevron_right

    
    

    Output:

    -- listing properties --
    ide=ide.geeksforgeeks.org
    
  8. void load(Reader reader): This method Reads a property list (key and element pairs) from the input character stream in a simple line-oriented format.

    Syntax: public void load(Reader reader).
    Returns: NA.
    Exception: IOException -- if an error occurred when reading from 
    the input stream.
    IllegalArgumentException -- if the input stream contains a malformed
     Unicode escape sequence.
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java code illustrating load() method
    import java.io.IOException;
    import java.io.StringReader;
    import java.util.*;
    class PropertiesDemo
    {
        public static void main(String arg[]) throws IOException
        {
            Properties gfg = new Properties();
            String s = "ide = ide.geeksforgeeks.org";
              
            // creating a new reader
            StringReader reader = new StringReader(s);
              
            // loading from input stream
            gfg.load(reader);
              
            gfg.list(System.out);
        }
    }

    chevron_right

    
    

    Output:

    -- listing properties --
    ide=ide.geeksforgeeks.org
    
  9. void store(OutputStream streamOut, String Description): After writing the string specified by description, the property list is written to the output stream linked to streamOut.

    Syntax: public void store(OutputStream streamOut, String Description).
    Returns: the previous value of the specified key 
    in this property list, or null if it did not have one.
    Exception: 
    IOException -- if writing this property list to the 
    specified output stream throws an IOException.
    ClassCastException -- if this Properties object contains 
    any keys or values that are not Strings.
    NullPointerException -- if out is null.
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java code illustrating store() method
    import java.io.IOException;
    import java.io.StringReader;
    import java.util.*;
    class PropertiesDemo
    {
        public static void main(String arg[]) throws IOException
        {
            Properties gfg = new Properties();
            gfg.put("ide", "ide.geeksforgeeks.org");
            gfg.put("contribute", "contribute.geeksforgeeks.org");
            gfg.put("quiz", "quiz.geeksforgeeks.org");
              
            // store the properties in output stream
            gfg.store(System.out, "Demo of Properties class");   
        }
    }

    chevron_right

    
    

    Output:

    #Demo of Properties class
    #Wed Jul 05 22:49:43 IST 2017
    contribute=contribute.geeksforgeeks.org
    quiz=quiz.geeksforgeeks.org
    ide=ide.geeksforgeeks.org
    
  10. void store(Reader reader, String Description): This method writes this property list (key and element pairs) in this Properties table to the output character stream in a format suitable for using the load(Reader) method.

    Syntax: public store(Reader reader, String Description)
    Returns: the previous value of the specified key in 
    this property list, or null if it did not have one.
    Exception: 
    IOException -- if writing this property list to 
    the specified output stream throws an IOException.
    ClassCastException -- if this Properties object contains 
    any keys or values that are not Strings.
    NullPointerException -- if out is null.
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java code illustrating store() method
    import java.io.IOException;
    import java.io.StringWriter;
    import java.util.*;
    class PropertiesDemo
    {
        public static void main(String arg[]) throws IOException
        {
            Properties gfg = new Properties();
            StringWriter writer = new StringWriter();
            gfg.put("ide", "ide.geeksforgeeks.org");
            gfg.put("contribute", "contribute.geeksforgeeks.org");
            gfg.put("quiz", "quiz.geeksforgeeks.org");
              
            gfg.store(writer, "Demo of Properties class");
              
            System.out.print(writer.toString());
        }
    }

    chevron_right

    
    

    Output:

    #Demo of Properties class
    #Wed Jul 05 22:55:16 IST 2017
    contribute=contribute.geeksforgeeks.org
    quiz=quiz.geeksforgeeks.org
    ide=ide.geeksforgeeks.org
    
  11. Set stringPropertyNames(): Returns a set of keys in this property list where the key and its corresponding value are strings, including distinct keys in the default property list if a key of the same name has not already been found from the main properties list. Properties whose key or value is not of type String are omitted.
    The returned set is not backed by the Properties object. Changes to this Properties are not reflected in the set, or vice versa.

    Syntax: public Set stringPropertyNames().
    Returns: a set of keys in this property list 
    where the key and its corresponding value are strings, including 
    the keys in the default property list.
    Exception: NA.
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java code illustrating stringPropertiesNames() method
    import java.io.IOException;
    import java.util.*;
    class PropertiesDemo
    {
        public static void main(String arg[]) throws IOException
        {
            Properties gfg = new Properties();
             
            gfg.put("ide", "ide.geeksforgeeks.org");
            gfg.put("contribute", "contribute.geeksforgeeks.org");
            gfg.put("quiz", "quiz.geeksforgeeks.org");
              
            // property name in the set
            Set set = gfg.stringPropertyNames();
              
            System.out.print(set);
        }
    }

    chevron_right

    
    

    Output:

    [contribute, quiz, ide]
    
  12. void loadFromXML(InputStream in): Loads all of the properties represented by the XML document on the specified input stream into this properties table.

    Syntax: public void loadFromXML(InputStream in).
    Returns: NA.
    Exception: 
    IOException - if reading from the specified input stream results in an IOException.
    InvalidPropertiesFormatException - Data on input stream 
    does not constitute a valid XML document with the mandated 
    document type.
    NullPointerException - if in is null.
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java code illustrating loadFromXML() method
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.*;
    class PropertiesDemo
    {
        public static void main(String arg[]) throws IOException
        {
            Properties gfg = new Properties();
             
            gfg.put("ide", "ide.geeksforgeeks.org");
            gfg.put("contribute", "contribute.geeksforgeeks.org");
            gfg.put("quiz", "quiz.geeksforgeeks.org");
              
            FileOutputStream out = new FileOutputStream("properties.xml");
            FileInputStream in = new FileInputStream("properties.xml");
              
            // store the properties into specified xml
            gfg.storeToXML(out, null);
              
            // load the properties from specified xml
            gfg.loadFromXML(in);
              
            gfg.list(System.out);
        }
    }

    chevron_right

    
    

    Output:

    -- listing properties --
    contribute=contribute.geeksforgeeks.org
    quiz=quiz.geeksforgeeks.org
    ide=ide.geeksforgeeks.org
    
  13. Run the java code for method no.- 13 and 14 on ide installed in your computer system for the output of code.

  14. void storeToXML(OutputStream os, String comment): This method emits an XML document representing all of the properties contained in this table.

    Syntax: public void storeToXML(OutputStream os, String comment)
    Returns: NA.
    Exception: 
    IOException -- if writing this property list to the 
    specified output stream throws an IOException.
    ClassCastException -- if this Properties object contains any 
    keys or values that are not Strings.
    NullPointerException -- if out is null.
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java method illustrating storeToXML() method
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.*;
    class PropertiesDemo
    {
        public static void main(String arg[]) throws IOException
        {
            Properties gfg = new Properties();
             
            gfg.put("ide", "ide.geeksforgeeks.org");
            gfg.put("contribute", "contribute.geeksforgeeks.org");
            gfg.put("quiz", "quiz.geeksforgeeks.org");
              
            FileOutputStream out = new FileOutputStream("properties.xml");
            FileInputStream in = new FileInputStream("properties.xml");
              
            // store the properties into specified xml
            gfg.storeToXML(out, "Demo of properties class");
              
            while(in.available()>0)
            {
            System.out.print((char)in.read());
            }
        }
    }

    chevron_right

    
    

  15. void storeToXML(OutputStream os, String comment, String encoding): This method emits an XML document representing all of the properties contained in this table, using the specified encoding.

    Syntax: public void storeToXML(OutputStream os, String comment, String encoding)
    Returns: NA.
    Exception: 
    IOException -- if writing this property list to 
    the specified output stream throws an IOException.
    ClassCastException -- if this Properties object 
    contains any keys or values that are not Strings.
    NullPointerException -- if out is null.
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java code illustrating storeToXML() method
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.*;
    class PropertiesDemo
    {
        public static void main(String arg[]) throws IOException
        {
            Properties gfg = new Properties();
             
            gfg.put("ide", "ide.geeksforgeeks.org");
            gfg.put("contribute", "contribute.geeksforgeeks.org");
            gfg.put("quiz", "quiz.geeksforgeeks.org");
              
            FileOutputStream out = new FileOutputStream("properties.xml");
            FileInputStream in = new FileInputStream("properties.xml");
              
            // store the properties into specified xml
            gfg.storeToXML(out, "Demo of properties class", "ISO-8859-3");
              
            while(in.available()>0)
            {
            System.out.print((char)in.read());
            }
        }
    }

    chevron_right

    
    

  16. void save(): This method does not throw an IOException if an I/O error occurs while saving the property list. Calls the store(OutputStream out, String comments) method and suppresses IOExceptions that were thrown.

    Syntax: public void save().
    Returns: NA.
    Exception: 
    ClassCastException - if this Properties object contains any keys
     or values that are not Strings.
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java code illustrating save() method
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.*;
    class PropertiesDemo
    {
        public static void main(String arg[]) throws IOException
        {
            Properties gfg = new Properties();
             
            gfg.put("ide", "ide.geeksforgeeks.org");
            gfg.put("contribute", "contribute.geeksforgeeks.org");
            gfg.put("quiz", "quiz.geeksforgeeks.org");
              
            FileOutputStream out = new FileOutputStream("properties.txt");
            FileInputStream in = new FileInputStream("properties.txt");
              
            // saving the properties in specified txt file
            gfg.save(out, null);
              
            gfg.load(in);
              
            gfg.list(System.out);
              
        }
    }

    chevron_right

    
    

    Output:

    -- listing properties --
    contribute=contribute.geeksforgeeks.org
    quiz=quiz.geeksforgeeks.org
    ide=ide.geeksforgeeks.org
    

This article is contributed by Abhishek Verma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.`



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.