tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Java > Properties > Load Properties

Load Properties 

Often instead of populating properties programatically, we use a text based external configuration or properties file. This example shows loading properties from a text file,

File Name  :  
com/bethecoder/tutorials/utils/props/LoadProps.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.utils.props;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

public class LoadProps {
  /**
   @param args
   */
  public static void main(String[] args) {

    Properties prop = new Properties();
    String propFile = "C:/prop_file.txt";

    try {
      System.out.println("Before properties load : " + prop);
      prop.load(new FileReader(propFile));
      System.out.println("After properties load : " + prop);
      
      //Access properties
      System.out.println(prop.getProperty("ONE"));
      System.out.println(prop.getProperty("TWO"));
      
    catch (FileNotFoundException e) {
      e.printStackTrace();
    catch (IOException e) {
      e.printStackTrace();
    }
  }
}
   

Properties file format is shown below,
File Name  :  
source/com/bethecoder/tutorials/utils/props/prop_file.txt 
#My test comments
#Sat Apr 02 13:46:37 IST 2011
TWO=222222
FOUR=444444
ONE=111111
THREE=333333

It gives the following output,
Before properties load : {}
After properties load : {TWO=222222, ONE=111111, FOUR=444444, THREE=333333}
111111
222222



 
  


  
bl  br