Read key from Properties - Java java.lang

Java examples for java.lang:System

Description

Read key from Properties

Demo Code


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import org.apache.log4j.PropertyConfigurator;

public class Main{
    public static void main(String[] argv) throws Exception{
        String key = "java2s.com";
        System.out.println(getConfigKey(key));
    }/*from   ww w  .j av  a2s .  c  om*/
    protected static Properties pro = null;
    protected static InputStream is = null;
    
    public static String getConfigKey(String key) {
        String strVal = "";
        try {
            is = new FileInputStream(new File("src/jdbc_config.properties"));
            pro = new Properties();
            pro.load(is);
            PropertyConfigurator.configure(pro);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        strVal = (String) pro.getProperty(key);
        return strVal;
    }
}

Related Tutorials