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:
- Properties(): This creates a Properties object that has no default values.
- 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-
-
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.
-
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.
// Java code illustrating getProperty() methodimportjava.util.*;classPropertiesDemo{publicstaticvoidmain(String arg[]){Properties gfg =newProperties();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 tableURL = 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 liststr = gfg.getProperty("articl","not found");System.out.println("The URL for article is "+ str);}}chevron_rightfilter_noneOutput:
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
-
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.
// Java code illustrating list() methodimportjava.util.*;classPropertiesDemo{publicstaticvoidmain(String arg[]){Properties gfg =newProperties();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_rightfilter_noneOutput:
-- listing properties -- contribute=contribute.geeksforgeeks.org quiz=quiz.geeksforgeeks.org ide=ide.geeksforgeeks.org
-
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.
// Java code illustrating list() methodimportjava.io.PrintWriter;importjava.util.*;classPropertiesDemo{publicstaticvoidmain(String arg[]){Properties gfg =newProperties();PrintWriter writer =newPrintWriter(System.out);gfg.put("ide","ide.geeksforgeeks.org");gfg.put("contribute","contribute.geeksforgeeks.org");gfg.put("quiz","quiz.geeksforgeeks.org");// printing list using PrintWriter objectgfg.list(writer);// flushing the streamwriter.flush();}}chevron_rightfilter_noneOutput:
-- listing properties -- contribute=contribute.geeksforgeeks.org quiz=quiz.geeksforgeeks.org ide=ide.geeksforgeeks.org
-
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.
// Java code illustrating propertyNames() methodimportjava.util.*;classPropertiesDemo{publicstaticvoidmain(String arg[]){Properties gfg =newProperties();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 elementsSystem.out.println(name.nextElement());System.out.println(name.nextElement());System.out.println(name.nextElement());}}chevron_rightfilter_noneOutput:
contribute quiz ide
-
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.
// Java code illustrating setProperty() methodimportjava.util.*;classPropertiesDemo{publicstaticvoidmain(String arg[]){Properties gfg =newProperties();gfg.put("ide","ide.geeksforgeeks.org");gfg.put("contribute","contribute.geeksforgeeks.org");gfg.setProperty("quiz","quiz.geeksforgeeks.org");System.out.println(gfg);}}chevron_rightfilter_noneOutput:
{contribute=contribute.geeksforgeeks.org, quiz=quiz.geeksforgeeks.org, ide=ide.geeksforgeeks.org} -
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.
// Java code illustrating load() methodimportjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.util.*;classPropertiesDemo{publicstaticvoidmain(String arg[])throwsIOException{Properties gfg =newProperties();String s ="ide = ide.geeksforgeeks.org";FileOutputStream out =newFileOutputStream("properties.txt");FileInputStream in =newFileInputStream("properties.txt");// write the property in the output stream fileout.write(s.getBytes());// load from input streamgfg.load(in);gfg.list(System.out);}}chevron_rightfilter_noneOutput:
-- listing properties -- ide=ide.geeksforgeeks.org
-
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.
// Java code illustrating load() methodimportjava.io.IOException;importjava.io.StringReader;importjava.util.*;classPropertiesDemo{publicstaticvoidmain(String arg[])throwsIOException{Properties gfg =newProperties();String s ="ide = ide.geeksforgeeks.org";// creating a new readerStringReader reader =newStringReader(s);// loading from input streamgfg.load(reader);gfg.list(System.out);}}chevron_rightfilter_noneOutput:
-- listing properties -- ide=ide.geeksforgeeks.org
-
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.
// Java code illustrating store() methodimportjava.io.IOException;importjava.io.StringReader;importjava.util.*;classPropertiesDemo{publicstaticvoidmain(String arg[])throwsIOException{Properties gfg =newProperties();gfg.put("ide","ide.geeksforgeeks.org");gfg.put("contribute","contribute.geeksforgeeks.org");gfg.put("quiz","quiz.geeksforgeeks.org");// store the properties in output streamgfg.store(System.out,"Demo of Properties class");}}chevron_rightfilter_noneOutput:
#Demo of Properties class #Wed Jul 05 22:49:43 IST 2017 contribute=contribute.geeksforgeeks.org quiz=quiz.geeksforgeeks.org ide=ide.geeksforgeeks.org
-
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.
// Java code illustrating store() methodimportjava.io.IOException;importjava.io.StringWriter;importjava.util.*;classPropertiesDemo{publicstaticvoidmain(String arg[])throwsIOException{Properties gfg =newProperties();StringWriter writer =newStringWriter();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_rightfilter_noneOutput:
#Demo of Properties class #Wed Jul 05 22:55:16 IST 2017 contribute=contribute.geeksforgeeks.org quiz=quiz.geeksforgeeks.org ide=ide.geeksforgeeks.org
-
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.
// Java code illustrating stringPropertiesNames() methodimportjava.io.IOException;importjava.util.*;classPropertiesDemo{publicstaticvoidmain(String arg[])throwsIOException{Properties gfg =newProperties();gfg.put("ide","ide.geeksforgeeks.org");gfg.put("contribute","contribute.geeksforgeeks.org");gfg.put("quiz","quiz.geeksforgeeks.org");// property name in the setSet set = gfg.stringPropertyNames();System.out.print(set);}}chevron_rightfilter_noneOutput:
[contribute, quiz, ide]
-
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.
// Java code illustrating loadFromXML() methodimportjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.util.*;classPropertiesDemo{publicstaticvoidmain(String arg[])throwsIOException{Properties gfg =newProperties();gfg.put("ide","ide.geeksforgeeks.org");gfg.put("contribute","contribute.geeksforgeeks.org");gfg.put("quiz","quiz.geeksforgeeks.org");FileOutputStream out =newFileOutputStream("properties.xml");FileInputStream in =newFileInputStream("properties.xml");// store the properties into specified xmlgfg.storeToXML(out,null);// load the properties from specified xmlgfg.loadFromXML(in);gfg.list(System.out);}}chevron_rightfilter_noneOutput:
-- listing properties -- contribute=contribute.geeksforgeeks.org quiz=quiz.geeksforgeeks.org ide=ide.geeksforgeeks.org
-
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.
// Java method illustrating storeToXML() methodimportjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.util.*;classPropertiesDemo{publicstaticvoidmain(String arg[])throwsIOException{Properties gfg =newProperties();gfg.put("ide","ide.geeksforgeeks.org");gfg.put("contribute","contribute.geeksforgeeks.org");gfg.put("quiz","quiz.geeksforgeeks.org");FileOutputStream out =newFileOutputStream("properties.xml");FileInputStream in =newFileInputStream("properties.xml");// store the properties into specified xmlgfg.storeToXML(out,"Demo of properties class");while(in.available()>0){System.out.print((char)in.read());}}}chevron_rightfilter_none -
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.
// Java code illustrating storeToXML() methodimportjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.util.*;classPropertiesDemo{publicstaticvoidmain(String arg[])throwsIOException{Properties gfg =newProperties();gfg.put("ide","ide.geeksforgeeks.org");gfg.put("contribute","contribute.geeksforgeeks.org");gfg.put("quiz","quiz.geeksforgeeks.org");FileOutputStream out =newFileOutputStream("properties.xml");FileInputStream in =newFileInputStream("properties.xml");// store the properties into specified xmlgfg.storeToXML(out,"Demo of properties class","ISO-8859-3");while(in.available()>0){System.out.print((char)in.read());}}}chevron_rightfilter_none -
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.
// Java code illustrating save() methodimportjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.util.*;classPropertiesDemo{publicstaticvoidmain(String arg[])throwsIOException{Properties gfg =newProperties();gfg.put("ide","ide.geeksforgeeks.org");gfg.put("contribute","contribute.geeksforgeeks.org");gfg.put("quiz","quiz.geeksforgeeks.org");FileOutputStream out =newFileOutputStream("properties.txt");FileInputStream in =newFileInputStream("properties.txt");// saving the properties in specified txt filegfg.save(out,null);gfg.load(in);gfg.list(System.out);}}chevron_rightfilter_noneOutput:
-- listing properties -- contribute=contribute.geeksforgeeks.org quiz=quiz.geeksforgeeks.org ide=ide.geeksforgeeks.org
Run the java code for method no.- 13 and 14 on ide installed in your computer system for the output of code.
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.`
Recommended Posts:
- Implement Quartet Class with Triplet Class in Java using JavaTuples
- Implement Triplet Class with Pair Class in Java using JavaTuples
- Implement Quintet Class with Quartet Class in Java using JavaTuples
- Implement Sextet Class from Quintet Class in Java using JavaTuples
- Implement Octet Class from Septet Class in Java using JavaTuples
- Implement Decade Class from Ennead Class in Java using JavaTuples
- Implement Ennead Class from Octet Class in Java using JavaTuples
- Implement Septet Class from Sextet Class in Java using JavaTuples
- Implement Pair Class with Unit Class in Java using JavaTuples
- Difference between Abstract Class and Concrete Class in Java
- Using predefined class name as Class or Variable name in Java
- Java.lang.Class class in Java | Set 2
- Java.lang.Class class in Java | Set 1
- Java.util.concurrent.RecursiveAction class in Java with Examples
- Java.util.BitSet class methods in Java with Examples | Set 2



