{"id":86,"date":"2006-08-12T14:23:57","date_gmt":"2006-08-12T12:23:57","guid":{"rendered":"https:\/\/bob-team.de\/wordpress\/?p=86"},"modified":"2006-08-12T14:23:57","modified_gmt":"2006-08-12T12:23:57","slug":"java-properties","status":"publish","type":"post","link":"https:\/\/bob-team.de\/wordpress\/2006\/08\/12\/java-properties\/","title":{"rendered":"Java Properties"},"content":{"rendered":"<p>Eine Benutzerkonfiguration wird zum Beginn einer Anwendung geladen. W\u00e4hrend ihres Laufs werden die verschiedensten Paramter ge\u00e4ndert und zum Ende gespeichert.<\/p>\n<p>F\u00fcr den Zugriff auf die Konfiguration werden Pfad, Name, Parameter-Schl\u00fcssel und die Konfiguration (<em>params<\/em>) selbst global festgelegt.<\/p>\n<p>[code lang=&#8220;java&#8220;]public static final String CONFIG_PATH =<br \/>\n      System.getProperty(&#8222;user.home&#8220;) +<br \/>\n      System.getProperty(&#8222;file.separator&#8220;) +<br \/>\n      &#8222;.app&#8220;;<\/p>\n<p>public static final String CONFIG_FILE =<br \/>\n      &#8222;convert.properties&#8220;;<\/p>\n<p>public static final String PARAM_XYZ = &#8222;param.xyz&#8220;;<\/p>\n<p>private Properties params = new Properties();[\/code]<\/p>\n<p>Beim Start der Anwendung wird gepr\u00fcft, ob das Verzeichnis f\u00fcr die Konfiguration vorhanden ist. Wenn dies nicht der Fall ist, wird das Verzeichnis angelegt und eine m\u00f6glicherweise vorhandene Konfiguration eingelesen.<\/p>\n<p>[code lang=&#8220;java&#8220;]try {<\/p>\n<p>   File confDir = new File(CONFIG_PATH);<br \/>\n   if(! confDir.exists()) confDir.mkdir();        <\/p>\n<p>   String config = CONFIG_PATH +<br \/>\n         System.getProperty(&#8222;file.separator&#8220;) +<br \/>\n         CONFIG_FILE;<\/p>\n<p>   File c = new File(config);<\/p>\n<p>   if( c.exists() ){<br \/>\n      FileInputStream in = new FileInputStream(config);<br \/>\n      params.load(in);<br \/>\n      in.close();<br \/>\n   }<\/p>\n<p>} catch (Exception e) {<br \/>\n   e.printStackTrace();<br \/>\n   System.exit(-1);<br \/>\n}[\/code]<\/p>\n<p>Nach dem Laden hat man \u00fcber den Parameter-Schl\u00fcssel Zugriff auf die Werte der Konfiguration. Ist ein Schl\u00fcssel nicht definiert, wird <em>null<\/em> geliefert.<\/p>\n<p>[code lang=&#8220;java&#8220;]params.getProperty(PARAM_XYZ)[\/code]<\/p>\n<p>Ge\u00e4ndert wird ein Parameter ebenfalls \u00fcber seinen Schl\u00fcssel.<\/p>\n<p>[code lang=&#8220;java&#8220;]params.setProperty(PARAM_XYZ, &#8222;abcd&#8220;);[\/code]<\/p>\n<p>Am Ende m\u00fcssen alle aktuellen Werte gespeichert werden.<\/p>\n<p>[code lang=&#8220;java&#8220;]try {<br \/>\n   String config = CONFIG_PATH +<br \/>\n         System.getProperty(&#8222;file.separator&#8220;) +<br \/>\n         CONFIG_FILE;;<\/p>\n<p>   FileOutputStream out = new FileOutputStream(config);<br \/>\n   properties.store(out, &#8222;Lokale Konfiguration&#8220;);<br \/>\n   out.close();<\/p>\n<p>} catch (Exception e) {<br \/>\n   e.printStackTrace();<\/p>\n<p>}[\/code]<\/p>\n<p>Beim Speichern ist zu beachten, das alle gewollten M\u00f6glichkeiten &#8211; das Programm zu Beenden &#8211; beachtet werden. So kommen in einem typischen GUI die Titelleiste, ein Men\u00fceintrag und ein Toolbar-Button in Frage.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Eine Benutzerkonfiguration wird zum Beginn einer Anwendung geladen. W\u00e4hrend ihres Laufs werden die verschiedensten Paramter ge\u00e4ndert und zum Ende gespeichert. F\u00fcr den Zugriff auf die Konfiguration werden Pfad, Name, Parameter-Schl\u00fcssel und die Konfiguration (params) selbst global festgelegt. [code lang=&#8220;java&#8220;]public static final String CONFIG_PATH = System.getProperty(&#8222;user.home&#8220;) + System.getProperty(&#8222;file.separator&#8220;) + &#8222;.app&#8220;; public static final String CONFIG_FILE = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-86","post","type-post","status-publish","format-standard","hentry","category-java","entry"],"_links":{"self":[{"href":"https:\/\/bob-team.de\/wordpress\/wp-json\/wp\/v2\/posts\/86","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bob-team.de\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bob-team.de\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bob-team.de\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bob-team.de\/wordpress\/wp-json\/wp\/v2\/comments?post=86"}],"version-history":[{"count":0,"href":"https:\/\/bob-team.de\/wordpress\/wp-json\/wp\/v2\/posts\/86\/revisions"}],"wp:attachment":[{"href":"https:\/\/bob-team.de\/wordpress\/wp-json\/wp\/v2\/media?parent=86"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bob-team.de\/wordpress\/wp-json\/wp\/v2\/categories?post=86"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bob-team.de\/wordpress\/wp-json\/wp\/v2\/tags?post=86"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}