JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Program: How to assign default values for unavailable keys in properties file?


Description:

The Properties class also provide a method, incase if the key is not available in the properties file, then you can specify the default value. Below example give sample code for this.


Code:
package com.java2novice.properties;

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

public class MyPropDefaultValue {

private Properties prop = null;
	
	public MyPropDefaultValue(){
		
		InputStream is = null;
		try {
			this.prop = new Properties();
			is = this.getClass().getResourceAsStream("/sample.properties");
			prop.load(is);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public String getPropertyValue(String key){
		return this.prop.getProperty(key, "Its Default Value");
	}
	
	public static void main(String a[]){
		
		MyPropDefaultValue mpc = new MyPropDefaultValue();
		System.out.println("db.host: "+mpc.getPropertyValue("db.tables"));
		System.out.println("db.user: "+mpc.getPropertyValue("db.user"));
	}
}

sample.properties
db.host=appdomain.java2novice.com
db.user=java2novice
db.password=mypassword
db.service=orcl

Output:
db.host: Its Default Value
db.user: java2novice
<< Previous Program | Next Program >>

List of Properties class sample programs:

  1. How to load Properties file from a file system?
  2. How to load Properties file from the classpath?
  3. How to load Properties file from a static block or static method?
  4. How to assign default values for unavailable keys in properties file?
  5. How to get all keys from properties file?
  6. How to create and store property file dynamically?
  7. How to store property file as xml file?
  8. How to load property file using class name in java?
Knowledge Centre
String Vs StringBuffer
We know that String is immutable object. We can not change the value of a String object once it is initiated. If we try to change the value of the existing String object then it creates new object rather than changing the value of the existing object. So incase, we are going to do more modificatios on String, then use StringBuffer. StringBuffer updates the existing objects value, rather creating new object.
Famous Quotations
I don’t know the key to success, but the key to failure is trying to please everybody.
-- Bill Cosby

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2022 by Nataraja Gootooru. All Rights Reserved.