Java – Récupérer un fichier properties
CodeDevFrançais
Dans la série code à garder sous la main : récupérer un fichier properties en Java :
/*********************************************************/
/* Récuperer un fichier properties */
/*********************************************************/
import java.io.InputStream;
import java.util.Properties;
public class LoadProperties {
private static Properties props = null;
private static final String FICHIER_PROPERTIES = "com/mestests/ejb/client/agent.properties";
// Singleton
private static LoadProperties instance = new LoadProperties();
public static LoadProperties getInstance() {
return instance;
}
private LoadProperties() {
this.init();
}
private void init() {
// Si le fichier de properties n'est pas chargé : on le charge
if (props == null) {
try {
InputStream is = LoadProperties.class.getResourceAsStream(FICHIER_PROPERTIES);
if (is == null) {
throw new Exception();
}
props = new Properties();
props.load(is);
}
catch (Exception e) {
System.out.println("Impossible de charger le fichier de configuration demandé !");
}
}
}
public String getProperty(String maClef) {
return props.getProperty(maClef);
}
}
Exemple de fichier properties:
|
1 2 |
maclef = mavaleur com.toto.security.aloha = une autre valeur comme celle-ci |
Acces par:
String maValeur = LoadProperties.getInstance().getProperty("maclef");
Leave a comment