datentraeger

geistige Umweltverschmutzung

Java XML Properties

December 25, 2011 Java

Properties kann man auch in XML-Files schreiben. Das geht ganz fein. Ich habs bei mir mal so eingerichtet, dass er erst mal checkt, ob’s denn schon ein Properties-File gibt:

File propfile = new File ("properties.xml");
boolean exists = propfile.exists();

Falls es noch nicht vorhanden ist, soll es mit “Default”-Werten erstellt werden

if (!exists) {
  properties.put("user", "username");
  properties.put("password", "password");
  ...usw...
  properties.storeToXML(new FileOutputStream("properties.xml),"irgendein-Kommentar");
  properties.clear();
}

ist es vorhanden, sollen natürlich die enthaltenen Werte verwendet werden:

} else {
  properties..loadFromXML(new FileInputStream("properties.xml"));
}

Zu guter Letzt kann man die Werte nun auslesen

String user = properties.get("user").toString();
String password = properties.get("password").toString();
...usw...

Bisher hatte ich das Problem, dass ich die Properties im Jar-File mitgespeichert hatte. Die konnten zwar wunderbar ausgelesen werden, jedoch konnte natürlich nichts ins Jar geschrieben werden:

Properties = new Properties();
properties.load(Connection.class.getResourceAsStream ("/resources/connection.properties"));
...
String user = properties.getProperty("user");
...

Git – lokale Changes verwerfen / überschreiben

Gnome-Shell

Search
Posts
  • tt-rss update
  • freerdp ERRCONNECT_TLS_CONNECT_FAILED
  • Windose – kein Internet
  • Rechner ohne IP
  • Goodbye Nvidia
Archives
Categories
Tags
3D-Printing Android Apple Arch BBOS/QNX cli Container Database Debian/GNU Linux Docker foo.li FSFE fvwm git Gnome Gnusocial Groupware irc IT Java LaTeX lvm Mail Mobile Movies Multimedia Music Nonsense Office Packaging PHP Puppet Redmine SQL Think-About Uncategorized Vaping vim Virtualisierung Web Win WTF www-links X11 XMPP
Meta
  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
Blogroll
  • Datentraeger Pics
  • foo.li systeme + software
  • GnuSocial.li
Proudly powered by WordPress | Theme: Doo by ThemeVS.