Menu Bar

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Friday, 7 October 2016

What is Properties Class In Java ?

This blog is all about to learn Core Java Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.

Previously we have discussed about
What is LinkedHashMap In Java ?
Here, we learn What is Properties Class In Java.But before starting we must know what is Map Interface In Java.?
Class Properties

All Implemented Interfaces :-

Declaration of Properties Class:
public class Properties
extends Hashtable<Object,Object>

Properties Class belongs to java.util.*; package.
Each key and its corresponding value in the property class is a string.
Properties class is inherits from Hashtable, and put and putAll methods can be applied to a Properties object. Their use is strongly discouraged as they allow the caller to insert entries whose keys or values are not Strings.The setProperty method should be used instead.

If the store or save method is called on a Properties object that contains a non-String key or value, the call will fail. Similarly, the call to the propertyNames or list method will fail if it is called on a Properties object that contains a non-String key.

Constructor and Description :

Properties()
Creates an empty property list with no default values.

Properties(Properties defaults)
Creates an empty property list with the specified defaults.

Methods and Description :

Method
Description
public void load(Reader r)
Reads a property list (key and element pairs) from the input character stream in a simple line-oriented format.
public void load(InputStream is)
Reads a property list (key and element pairs) from the input byte stream.
public String getProperty(String key)
Searches for the property with the specified key in this property list.
public void setProperty(String key,String value)
Searches for the property with the specified key in this property list.
public void store(Writer w, String comment)
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.
public void store(OutputStream os, String comment)
Writes this property list (key and element pairs) in this Properties table to the output stream in a format suitable for loading into a Properties table using the load(InputStream) method.
storeToXML(OutputStream os, String comment)
Emits an XML document representing all of the properties contained in this table.
public void storeToXML(Writer w, String comment, String encoding)

Emits an XML document representing all of the properties contained in this table, using the specified encoding.

Example of Properties Class :-

In this example you will see how to read values from Properties Class, Properties File ,How to create and write keys and values in Properties File and How to Read system properties using Properties class.In this example each methods show you the different use of Properties class.

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;

public class PropertiesClassExample {
public static void readFromPropertiesClass(){
System.out.println("--READING DETAILS FROM PROPERTIES CLASS --");
Properties p = new Properties();
p.setProperty("url", "jdbc.connection.url");
p.setProperty("username", "pushkar");
p.setProperty("password", "pushkar123");
System.out.println("URL:\t\t"+p.getProperty("url"));
System.out.println("USERNAME:\t"+p.getProperty("username"));
System.out.println("PWD:\t\t"+p.getProperty("password"));
}
public static void createPropertiesFile(String filePath) throws IOException{
System.out.println("\n--EXAMPLE TO CREATE PROPERTIES FILE--");
Properties p = new Properties();
p.setProperty("url", "jdbc.connection.url");
p.setProperty("username", "pushkar");
p.setProperty("password", "pushkar123");
p.store(new FileWriter(filePath), "User can pass the comments in file also from here.");
System.out.println("file created successfully.");
}
public static void readFromPropertiesFile(String filePath ) throws IOException{
System.out.println("\n--READING DETAILS FROM .PROPERTIES FILE--");
FileReader fr = new FileReader(filePath);
Properties p1 = new Properties();
p1.load(fr);
System.out.println("USERNAME:\t"+p1.getProperty("username"));
System.out.println("PASSWORD:\t"+p1.getProperty("password"));
}
public static void readSystemProperties(){
System.out.println("\n--READING SYSTEM DETAILS USING PROPERTIES CLASS--");
Properties sys = System.getProperties();
Set<?> set = sys.entrySet();
set.forEach(s -> System.out.println(s));
}
public static void main(String[] args) throws IOException {
String filePath = "G:/info.properties";
PropertiesClassExample.readFromPropertiesClass();
PropertiesClassExample.createPropertiesFile(filePath);
PropertiesClassExample.readFromPropertiesFile(filePath);
PropertiesClassExample.readSystemProperties();
}
}

Program Output :-

--READING DETAILS FROM PROPERTIES CLASS --
URL: jdbc.connection.url
USERNAME: pushkar
PWD: pushkar123

--EXAMPLE TO CREATE PROPERTIES FILE--
file created successfully.

--READING DETAILS FROM .PROPERTIES FILE--
USERNAME: pushkar
PASSWORD: pushkar123

--READING SYSTEM DETAILS USING PROPERTIES CLASS--
java.runtime.name=Java(TM) SE Runtime Environment
sun.boot.library.path=C:\Program Files\Java\jre1.8.0_91\bin
java.vm.version=25.91-b14
java.vm.vendor=Oracle Corporation
java.vendor.url=http://java.oracle.com/
path.separator=;
java.vm.name=Java HotSpot(TM) 64-Bit Server VM
file.encoding.pkg=sun.io
user.country=US
user.script=
sun.java.launcher=SUN_STANDARD
sun.os.patch.level=
.................................




Share this Blog with yours Friends !!

No comments:

Post a Comment