/** * Classe : Configuration * * Description : * La classe Configuration va obtenir et mettre a jour les infos de * configuration a partir des fichiers de configuration * stockes sous [rep_courant]/config * Le fichier doit existe ou une exception sera levee. * * date : 8/05/2004 * @author : PeeX Team * @version : 1.0 * */ import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.Properties; import java.lang.Exception; import javax.swing.*; import java.io.File; import java.io.OutputStreamWriter; import java.io.FileOutputStream; public class Configuration { /** * Methode : getConfig * * Description : * La methode getConfig va retourner l'information de configuration * desiree a partir d'un fichier de configuration * * date : 8/05/2004 * @param : String fichier : Le nom du fichier de configuration * @param : String key : La cle dont on veut obtenir la valeur * * @return : String representant la valeur de l'info * */ public static String getConfig(String fichier, String key,String Default) { String leFichier = System.getProperty("user.dir") +System.getProperty("file.separator")+ fichier; String tmp = null; try { FileInputStream fis = new FileInputStream(leFichier); Properties config = new Properties(); config.load(fis); tmp = config.getProperty(key); fis.close(); leFichier = null; fis = null; config = null; } catch (Exception e) { return Default; } if (tmp == null) { return Default; } return tmp; } /** * Methode : setConfig * * Description : * La methode setConfig va mettre a jour/ inserer l'information de configuration * desiree a partir dans un fichier de configuration * * date : 8/05/2004 * @param : String fichier : Le nom du fichier de configuration * @param : String key : La cle dont on veut obtenir la valeur * @param : String valeur : La valeur associee a la cle * * @return : String representant la valeur de l'info * */ public static void setConfig(String fichier, String key, String valeur) throws Exception { // La petite feinte : Il faur recharger entierement le fichier // et le reecrire. //On construit l'adresse du fichier //String leFichier = System.getProperty("user.dir") + "/config/" + fichier; String leFichier = System.getProperty("user.dir")+System.getProperty("file.separator")+ fichier; //aixo ho he hagut d'afegir ja que si no existia el fitxer no funcionava. Aixo el crea quan no hi es File f = new File(leFichier); if (f.exists()==false) { OutputStreamWriter outputStream = null; try { outputStream = new OutputStreamWriter(new FileOutputStream(leFichier),"iso-8859-1"); outputStream.write(""); outputStream.close(); } catch (Exception e) { } } f=null; // On fait pointer notre Properties sur le fichier Properties config = new Properties(); FileInputStream fis = new FileInputStream(leFichier); config.load (fis); fis.close(); FileOutputStream fos = new FileOutputStream(leFichier); config.setProperty(key,valeur); config.store (fos,"Ultima modificacio :"); // C'est important de mettre a null, le garbage collector // passe plus vite ! fos.close(); leFichier = null; fos = null; fis = null; config = null; } }