Properties Files in Java

Advertisements

Prev Tutorial Next Tutorial

Properties Files in Collection Framework

A property file is one type of the file which organizing the data in the form of (key, value) pair. A property file is a text file which is to be created in any editors like-notepad, Editpuls and etc. Property file should be saved on same file name with on extension .prop or .rbf.

Properties Files Important points

  • A property file is a text file which is to be created in any editors like-notepad, Editpuls and etc.
  • Property file should be saved on same file name with on extension .prop or .rbf.
  • Properties class object organizes the data in the form of (key, value) pair and it displays in the same order in whichever order it is added.
  • Property file always resides in secondary memory.

java.util.properties

It is one of the pre-defined sub-class of Hashtable class, so that all the methods of Hashtable are inherited into properties class. Creating Properties is nothing but creating an object of properties class.

Syntax

Properties p=new Properties();
// Here p resides in heap memory.

Advantages of Properties class over Hashtable class

  • Properties class object is able to read the data from properties/resource bundle file.
  • Properties class always makes us to develop flexible application.

Constructor and Method of Properties class

Constructorperporties()
methodspublic void load(FileInputStream);
public String get Property(String);

Constructor of properties class is used for creating an object of properties class.

Syntax

Properties p=new Properties();

public void load(FileInputStream); is used for loading the content of property file ito properties class object by opening the properties file in read mode with the help of FileInputStream class.

Example

FileInputStream fis=new FileInputStream("student properties");
         p.load(fis);

public String get Property(String); is used for obtaining the value of value by passing the key.

properties file

Example

String sno=p.getProperty("stno");
String sname=p.getProperty("sname");
String marks=p.getProperty("marks");
properties file

Example of properties file

import java.util.*;
import java.io.*;
class Prop
{
public static void main(String []args)
{
try
{
Scanner s=new Scanner(System.in);
System.out.pritnln("Enter the property file name");
String pfile=s.nextLine();
FileInputStream fis=new FileInputStream();
Properties p=new Properties();
p.load(fis);
String sno=p.getProperty();
String sname=p.getProperty();
String marks=p.getProperty();
System.out.println("student number="+sno);
System.out.println("student name="+sname);
System.out.println("student marks="+marks);
fis.close();
}
catch(FileNotFoundException fe)
{
System.out.println("properties file does not exists");
}
catch(Exception e)
{
System.out.println(e);
}
}
}

Prev Tutorial Next Tutorial

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements