// Class public class Prop { private String basePathProp = ".property"; File fichier; FileWriter ff; private Properties properties = null; public Prop() { init(); try { loadProperties(basePathProp); } catch (Exception e) { JOptionPane.showMessageDialog(null, "erreur in loading the property file \n" + e.getMessage()); } } private void init() { fichier = new File(basePathProp); // si le fichier est present if (!fichier.exists() || !fichier.canRead()) { try { ff = new FileWriter(fichier); ff.write( "lang=Langage\n" + "desc=<html> Description :</html>\n" + "1 =XMLBase\n" + "2=Java-Servlet-JSP\n"); } catch (Exception ex) { JOptionPane.showMessageDialog(null, "erreur initialisation....\n" + ex.getMessage()); } finally { try { ff.close(); } catch (IOException ex) { JOptionPane.showMessageDialog(null, "Erreur in Closing ... \n" + ex.getMessage()); } } } } private void loadProperties(String propertiesFileName) throws FileNotFoundException { properties = new Properties(); FileInputStream fileInputStream = new FileInputStream(propertiesFileName); try { properties.load(fileInputStream); } catch (IOException e) { e.printStackTrace(); } finally { try { fileInputStream.close(); } catch (Exception e) { } } } public String get(String s) { return properties.getProperty(s); } } // dans le main public static void main(String[] arg) { Prop p = new Prop(); System.out.println("Code=" + p.get("lang")); System.out.println("testing Properties"); }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question