Properties Class Inside Java Language

What Is Properties Class In Java?

Properties class in java uses both key and value pair as a string. Thus, the properties class can be used to store data. this class can be used to get data from properties file and store data to properties file. It can be used to get the Properties of System as well.

The Advantage Of Properties File

It’s a properties class file, thus it requires no recompilation. So if there is a change of information from the properties file. As a result java class doesn’t need to recompile by the user. In addition the programs frequently changed the information inside this properties class.

Method Of Class

As a result these method below is a list of common used method in properties class :

  • public void load(Reader r) : loads data from the Reader object.
  • public void load(InputStream is) : loads data from the InputStream object
  • public String getProperty(String key) : returns value based on the key.
  • public void setProperty(String key,String value) : sets the property in the properties object.
  • public void store(Writer w, String comment) : writers the properties in the writer object.
  • storeToXML(OutputStream os, String comment) : writers the properties in the writer object for generating xml document.
  • public void store(OutputStream os, String comment) : writes the properties in the OutputStream object.
  • public void storeToXML(Writer w, String comment, String encoding) : writers the properties in the writer object for generating xml document with specified encoding.

Finally, here is an example of properties class. Also it is getting information  from properties file :

in addition, we need to create a properties file first :

user=system
password=oracle

Then, we can create the java class to read from file :

import java.util.*;
import java.io.*;
public class Test {
public static void main(String[] args)throws Exception{
FileReader reader=new FileReader(“db.properties”);

Properties p=new Properties();
p.load(reader);

System.out.println(p.getProperty(“user”));
System.out.println(p.getProperty(“password”));
}
}

Finally, it will output :

System

Oracle

The programming language that I specialize in is Java, because I think Java programming language is more universal and of course because I like it regardless of any reason.

Published by

Rafif Santika

The programming language that I specialize in is Java, because I think Java programming language is more universal and of course because I like it regardless of any reason.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.