Java.util - SPLessons

Java.util Properties

Home > Lesson > Chapter 13
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.util Properties

Java.util Properties

shape Description

The properties protest will have esteem and key consolidate both as a string. It can be used to get the estimation of property in perspective of the property key. The Properties class offers procedures to get data from properties report and store data into properties record. Moreover, it can be used to get properties of the system. The java.util.Properties class is the subclass of Hashtable. The essential preferred standpoint of java.util.Properties class is that recompilation is not required. Following is the syntax declaration of the class. [java] public class Properties extends Hashtable<Object,Object> [/java]

Constructors Of java.util Properties Class

shape Constructors

Following are the constructors of the java.util Properties Class.
Constructor Description
Properties() To make an empty property list without default values.
Properties(Properties) To make an empty property list with default values.

Methods Of java.util Properties Class

shape Methods

Following are the methods of the java.util Properties Class.
Methods Description
getProperty(String) To hunt down the property with the predetermined key in the property list.
list(PrintStream) To list out the output stream.
list(PrintWriter) To list out the output stream.
load(InputStream) To read the list of the property from input stream.
propertyNames() An enumeration of all the keys will be returned.

By Using java.util.Properties.getProperty()

shape Description

Following is the syntax declaration of the method. [java]public String getProperty(String key)[/java] DemoProperties.java [java] package com.SPlessons; import java.util.*; public class DemoProperties { public static void main(String[] args) { Properties prop = new Properties(); // add some properties prop.put("SPlessons", "200"); prop.put("splessons", "150"); prop.put("Scannable", "true"); // get two properties and print them System.out.println("" + prop.getProperty("SPlessons")); System.out.println("" + prop.getProperty("Scannable")); } } [/java] In the above example, for the given key corresponding value is going to be generated in the output. Output: Now compile the code result will be as follows. [java] 200 true [/java]

By Using java.util.Properties.list(PrintStream out)

shape Description

Following is the syntax declaration of the method. [java]public void list(PrintStream out)[/java] DemoProperties.java [java] package com.SPlessons; import java.util.*; public class DemoProperties { public static void main(String[] args) { Properties prop = new Properties(); // add some properties prop.put("SPlessons", "200"); prop.put("splessons", "150"); prop.put("Scannable", "true"); // print the list with System.out prop.list(System.out); } } [/java] In the above example, System.out is used to display the elements which were in the list. Output: Now compile the code result will be as follows. [java] -- listing properties -- splessons=150 SPlessons=200 Scannable=true [/java]

By Using java.util.Properties.list(PrintWriter out)

shape Description

Following is the syntax declaration of the method. [java]public void list(PrintWriter out)[/java] DemoProperties.java [java] package com.SPlessons; import java.io.PrintWriter; import java.util.*; public class DemoProperties { public static void main(String[] args) { Properties prop = new Properties(); PrintWriter writer = new PrintWriter(System.out); // add some properties prop.put("SPlessons", "200"); prop.put("splessons", "150"); prop.put("Scannable", "true"); // print the list with a PrintWriter object prop.list(writer); // flush the stream writer.flush(); } } [/java] In the above example, PrintWriter belongs to java.io package. The java.util.Properties.list() method does not return any value. Output: Now compile the code result will be as follows. [java] -- listing properties -- splessons=150 SPlessons=200 Scannable=true [/java]

By Using java.util.Properties.load()

shape Description

Following is the syntax declaration of the method. [java]public void load(InputStream inStream)[/java] DemoProperties.java [java] package com.SPlessons; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintWriter; import java.util.*; public class DemoProperties { public static void main(String[] args) { Properties prop = new Properties(); String s = "SPLESSONS=200"; String s2 = "splessons=15"; try { // create a new input and output stream FileOutputStream fos = new FileOutputStream("properties.txt"); FileInputStream fis = new FileInputStream("properties.txt"); // write the first property in the output stream file fos.write(s.getBytes()); // change the line between the two properties fos.write("\n".getBytes()); // write next property fos.write(s2.getBytes()); // load from input stream prop.load(fis); // print the properties list from System.out prop.list(System.out); } catch (IOException ex) { ex.printStackTrace(); } } } [/java] Following is a method explanation diagram. Output: Now compile the code result will be as follows. [java] -- listing properties -- splessons=15 SPLESSONS=200 [/java]

By Using java.util.Properties.propertyNames()

shape Description

Following is the syntax declaration of the method. [java]public Enumeration<?> propertyNames()[/java] DemoProperties.java [java] package com.SPlessons; import java.util.*; public class DemoProperties { public static void main(String[] args) { Properties prop = new Properties(); // add some properties prop.put("WWW.SPLESSONS.COM", "320"); prop.put("www.splessons.com", "215"); // assign the property names in a enumaration Enumeration<?> enumeration = prop.propertyNames(); // print the enumaration elements System.out.println("" + enumeration.nextElement()); System.out.println("" + enumeration.nextElement()); } } [/java] In the above example, enumeration of all keys will be displayed. Output: Now compile the code result will be as follows. [java] WWW.SPLESSONS.COM www.splessons.com [/java]

Summary

shape Key Points

  • The java.util.Properties.list() method is utilized for debugging.
  • An enumeration of all the keys will be returned by java.util.Properties.propertyNames() method