Eine java.util.Properties Datei ist ein Schlüssel, Werte basierte ASCII Datei.
Die Struktur kann XML basiert oder mit einem Gleichheitszeichen „=“ getrennt sein.
Hier das Beispiel für das Speichern in einer Properties Datei im einfachen Format:
import java.io.*;
import java.util.*;
import java.lang.Exception;
public class EinstellungHandler {
private static final String FILENAME = "./einstellung.propertie";
private static final String LANG_KEY = "lang";
private static Properties properties;
private static Einstellung einstellung;
public static void main(String... args) {
einstellung = new Einstellung();
readPropertie();
einstellung.setSprache("deutsch");
properties.put(LANG_KEY, einstellung.getSprache());
writePropertie();
}
private static void readPropertie() {
properties = new Properties();
FileInputStream fileInStream = null;
BufferedInputStream buffInStream = null;
try {
fileInStream = new FileInputStream(FILENAME);
buffInStream = new BufferedInputStream(fileInStream);
properties.load(buffInStream);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileInStream != null) {
try {
fileInStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (buffInStream != null) {
try {
buffInStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
einstellung.setSprache(properties.getProperty(LANG_KEY));
}
private static void writePropertie() {
FileOutputStream fileOutStream = null;
try {
fileOutStream = new FileOutputStream(FILENAME);
properties.store(fileOutStream, null);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileOutStream != null) {
try {
fileOutStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private static class Einstellung implements Serializable {
private static final long serialVersionUID = 1l;
private String sprache;
public String getSprache() {
return this.sprache;
}
public void setSprache(String inSprache) {
this.sprache = inSprache;
}
}
}
Letzte Aktualisierung am: 02. Mai 2023