Properties

Properties

It is the child class of Hashtable. In this case both key and value should be String Object only.

Constructor

Properties p = new Properties ()

Important methods of Properties

  1. String getProperty (String name). Return the value associated with the specified Property. If the specified property is not available then we will get null.
  2. String setProperty (String Pname, String Pvalue). If the specified Property is already available then the old value is replaced with new value and old value will be return.
  3. Enumeration PropertyName();
  4. Void load (InputStream in); // to load Properties from properties file to java Properties Object.
  5. void store(outputStream out, String commments) the Properties from java Properties Object to Properties file.

PropertiesDemo.java

package com.narayanatutorial.collections.properties;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class PropertiesDemo {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		Properties p = new Properties();
		FileInputStream fis = new FileInputStream("/home/narayana/Documents/abc.properties");
		p.load(fis);
		System.out.println("propety file data:"+p);
		String s = p.getProperty("tutorialName");
		System.out.println("tutorialName parameter value:"+s);
		p.setProperty("Location", "Bangalore");
		FileOutputStream fos = new FileOutputStream("/home/narayana/Documents/abc.properties");
		p.store(fos, " updated by Narayanatutorial");
		String loc=p.getProperty("Location");
		System.out.println("Location parameter value:"+loc);
		
	}

}

Output

propety file data:{tutorialName=narayanatutorial}
tutorialName parameter value:narayanatutorial
Location parameter value:Bangalore

abc.properties file output

# updated by Narayanatutorial
#Mon Feb 25 05:07:16 IST 2019
Location=Bangalore
tutorialName=narayanatutorial

Reference Links

https://docs.oracle.com/javase/6/docs/api/java/util/package-summary.html

Leave a Reply