JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Checkout for Promo Codes

Program: How to load Properties file from a file system?


Description:

This example shows how to load properties file from a local file system. Here we are using FileInputStream to load the properties file. Here you have to pass the physical location of the file in the file system.


Code:
package com.java2novice.properties;

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

public class MyPropertyLoad {

	public static void main(String a[]){
		
		InputStream is = null;
		Properties prop = null;
		try {
			prop = new Properties();
			is = new FileInputStream(new File("C:/sample.properties"));
			prop.load(is);
			System.out.println("db.host: "+prop.getProperty("db.host"));
			System.out.println("db.user: "+prop.getProperty("db.user"));
			System.out.println("db.password: "+prop.getProperty("db.password"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

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

Output:
db.host: appdomain.java2novice.com
db.user: java2novice
db.password: mypassword
 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
What is adapter class?
An adapter class provides the default implementation of all methods in an event listener interface. Adapter classes are very useful when you want to process only few of the events that are handled by a particular event listener interface. You can define a new class by extending one of the adapter classes and implement only those events relevant to you.
Famous Quotations
I respect faith, but doubt is what gets you an education.
-- Wilson Mizner

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 © 2020 by Nataraja Gootooru. All Rights Reserved.