The properties file needs to reside anywhere on the classpath (use src/main/java)
1) create this class and pass the name of your properties file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ContactUtils; | |
import java.io.InputStream; | |
import java.util.Properties; | |
public class PropertiesLookup { | |
Properties prop = new Properties(); | |
String filename = "config.properties"; | |
InputStream input = null; | |
public PropertiesLookup() { | |
try { | |
input = getClass().getClassLoader().getResourceAsStream(filename); | |
if (input == null) { | |
System.out.println("Sorry, unable to find " + filename); | |
return; | |
} | |
prop.load(input); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
public String getProperty(String key) { | |
return prop.getProperty(key); | |
} | |
} | |
package com.client; | |
import ContactUtils.PropertiesLookup; | |
public class GarbageRun { | |
public static void main(String[] args) { | |
PropertiesLookup pl = new PropertiesLookup(); | |
String pValue = pl.getProperty("property1"); | |
System.out.println(pValue); | |
} | |
} | |
config.properties | |
property1=myname | |
property2 =mylastname |
https://gist.github.com/kbilel/90b0d167b6fd17c2f830b9b12c60e947.js