Using properties file in java application

Written by bharat.sharma on . Posted in Core Java, How to?

Properties files are a cool place to store your configuration details like database connection properties, error messages and other configurations for your program.

You can use properties file in your application using following utility class. This class basically loads your properties file on first attempt to access any property on further attempts to access any property it will return properties already loaded in memory and not attempt to read file again and again.

Static getProperty(String key) method allows you to get property value by passing the key and static method Set<Object> getkeys() returns a set of keys in the file.

package com.bharat.utils;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.Set;

public class PropertiesUtil
{

  private static Properties props;

  static
  {
    props = new Properties();
    try
    {
      PropertiesUtil util = new PropertiesUtil();
      props = util.getPropertiesFromClasspath("placeholder.properties");
    }
    catch (FileNotFoundException e)
    {
      e.printStackTrace();
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
  }

  // private constructor
  private PropertiesUtil()
  {
  }

  public static String getProperty(String key)
  {
    return props.getProperty(key).trim();
  }

  public static Set getkeys()
  {
    return props.keySet();
  }

  /**
   * loads properties file from classpath
   * 
   * @param propFileName
   * @return
   * @throws IOException
   */
  private Properties getPropertiesFromClasspath(String propFileName)
                                                                    throws IOException
  {
    Properties props = new Properties();
    InputStream inputStream =
        this.getClass().getClassLoader().getResourceAsStream(propFileName);

    if (inputStream == null)
    {
      throw new FileNotFoundException("property file '" + propFileName
          + "' not found in the classpath");
    }

    props.load(inputStream);
    return props;
  }
}

You may put your properties file anywhere in project’s classpath.

References:

  1. http://docs.oracle.com/javase/6/docs/api/java/util/Properties.html
  2. http://bharatonjava.wordpress.com/2012/09/12/using-properties-file-in-java-application/

How to in Oracle

Written by admin on . Posted in How to?

Here we will find the answer of all the How TO question in Oracle Database.

How to check the version of Oracle DB? ? 

select banner from v$version

 

How to create synonym? 

create synonym v_table for jbt_table
create public synonym v_table for JBT_function

 

How to check the current date/month.year? 

select to_char(sysdate, 'dd') from dual
select to_char(sysdate, 'yyyy') Year from dual
select to_char(sysdate, 'mon') Year from dual

 

How to Extend or Decrease the Size of a Tablespace in Oracle?

 

Java How to?

Written by admin on . Posted in How to?

How to reverse a String in Java?

How to split string in java(By Spaces) in Java?

How to convert string array to int array in Java?

How convert string array to string in Java?

How convert string array to char array in Java

How to convert string to int in Java?

How to convert string to datetime in Java?

How to convert string to byte array in Java?

How to convert stringbuilder to string in Java?

How to convert string to StringBuilder in Java?

How to check if char is null in Java?

How to check if char is letter Java?

How to check if char array is empty in Java?

How to check if char is number in Java?

How to remove string from string in Java?

How to remove string from array?

How to remove string from arraylist in Java?

How to remove string from stringbuilder?

How to check string equality in Java?

How to reverse a String in Java?

How to reverse StringBuilder in Java?

How to check if string contains substring in java?

 

Oracle How to ?

Written by admin on . Posted in How to?

Here we will find the answer of all the How TO question in Oracle Database.

How to check the version of Oracle DB? ?

select banner from v$version

                     How to create synonym?

create synonym v_table for jbt_table
create public synonym v_table for JBT_function

                     How to check the current date/month.year?

select to_char(sysdate, 'dd') from dual
select to_char(sysdate, 'yyyy') Year from dual
select to_char(sysdate, 'mon') Year from dual

Java Date To String Conversion

Written by admin on . Posted in How to?

public class DateToString {

 public static void main(String args[]) {
  SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
  SimpleDateFormat sdf2 = new SimpleDateFormat();
  Date date = new Date();
  String strDate1 = sdf1.format(date);
  String strDate2 = sdf2.format(date);
  System.out.println("Value of the Converted Date :" + strDate1);
  System.out.println("Value of the Converted Date :" + strDate2);
 }
}

 

Java String To Date Conversion

Written by admin on . Posted in How to?

public class StringToDate {

 public static void main(String args[]) throws ParseException {
  SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
  String dateStr = "10-Feb-2012 17:45:25";
  Date date = sdf.parse(dateStr);
  System.out.println(sdf.parse(dateStr));
 }
}

 

Convert list to array in java

Written by admin on . Posted in How to?

public class ListToArray {

 public static void main(String args[])
 {
  List list= new ArrayList();
  list.add("1");
  list.add("2");
  list.add("3");

  Object[] intArr = list.toArray();

  System.out.println("Value inf String Array is : "+intArr[0]+"-"+intArr[1]+"-"+intArr[2]);

 }
}