How to Load Properties from Text File in Java ?

Below code shows how to load properties from text file in java –

/****************************************************************************************
 * Created on 08-2011 Copyright(c) https://kodehelp.com All Rights Reserved.
 ****************************************************************************************/
package com.kodehelp.java.io;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;

/**
 * Created by Vigilance India.
 * User: https://kodehelp.com
 * Date: Aug 4, 2011
 */
public class LoadPropertiesFromTextFile {
     public static void main(String args[]){
        Properties prop = new Properties();
        try {
            FileInputStream fileInputStream = new FileInputStream("/config.txt");
            prop.loadFromXML(fileInputStream);
            prop.list(System.out);
            String userName = prop.getProperty("USERNAME");
            System.out.println("USERNAME ::"+userName);
            String password = prop.getProperty("PASSWORD");
            System.out.println("PASSWORD ::"+password);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (InvalidPropertiesFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

USERNAME=kodehelp PASSWORD=secret

You can also check example for loading properties from XML file

Please Post Your Comments & Reviews

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.