GET PROPERTIES FILE KEY/VALUES in java


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

 


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

view raw

getproperties

hosted with ❤ by GitHub

 

https://gist.github.com/kbilel/90b0d167b6fd17c2f830b9b12c60e947.js


Votre commentaire

Entrez vos coordonnées ci-dessous ou cliquez sur une icône pour vous connecter:

Logo WordPress.com

Vous commentez à l’aide de votre compte WordPress.com. Déconnexion /  Changer )

Image Twitter

Vous commentez à l’aide de votre compte Twitter. Déconnexion /  Changer )

Photo Facebook

Vous commentez à l’aide de votre compte Facebook. Déconnexion /  Changer )

Connexion à %s