Konfigurationsdatei für Java-App

Webmaster Pazi

Aktives Mitglied
Hallöchen, habe wieder mal eine Frage an euch 🙂

Da meine Java-Application jetzt auch eine Konfigurationsdatei braucht, habe ich mich zuerst an
die Properties-Klasse versucht.

Da scheint richtig zu zu funktionieren, jedoch, wenn ich einen Wert ändern will muss ich wieder
alle Werte hinzufügen (beim Speichern).

Laut Java-API gibts eine Methode Namens propertyNames() diese Methode listet alle vorhandenen
Einträge auf.

Code:
public Enumeration propertyNames()

Jetzt stehe ich aber am Schlauch, ich frag mich was das auf sich hat mit Enumeration.
Kann mir jemand da weiterhelfen? Oder würde sich für eine Konfigurationsdatei besser XML eignen?
 
Zeig mal den Code wie du die Properties lädst und wieder speicherst.
Eigentlich brauchst du nicht wieder alle Werte setzen vorm speichern.
 
Hallo, danke für die schnelle rückmeldung 🙂

Soo... Folgender Code is das Problem 😉

Java:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;


public class TestConfig {
    
    public FileInputStream input;
    public FileOutputStream output;
    public Properties property = new Properties();
    
    public TestConfig() {
        try {
            input = new FileInputStream(System.getProperty("user.home") + "/settings.cfg");
            output = new FileOutputStream(System.getProperty("user.home") + "/settings.cfg");
            property.load(input);
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        }
        
    }
    
    public String getValue(String key) {
        return property.getProperty(key);
    }
    
    public void setValue(String key, String value) {
        property.setProperty(key, value);
    }
    
    public void saveValue() {
        try {
            property.store(output, null);
        } catch (IOException e) {
        }
    }
    
    public static void main(String[] args) {
        TestConfig config = new TestConfig();
        config.setValue("MyKey", "MyValue");
        config.saveValue();
        System.out.println(config.getValue("MyKey"));
        System.out.println(config.getValue("MyKey2"));   //Gibt null aus
        System.out.println(config.getValue("MyKey3"));   //Gibt null aus
    }
    
}

Datei settings.cfg:
Code:
MyKey=MyValue
MyKey2=MyValue2
MyKey3=MyValue3
 
Böse!!
Java:
...
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        }
...  

        } catch (IOException e) {
        }
...
Gut:
Java:
...
        } catch (FileNotFoundException e) {
               e.printStackTrace();
        } catch (IOException e) {
               e.printStackTrace();
        }
...  

        } catch (IOException e) {
               e.printStackTrace();
        }
...
Vermutlich treten Fehler auf, die Du nicht mitbekommst.
 
was soll output da?
die streams solltest du danach schließen. ansonsten sollte das so funktionieren.
 
Hmm auch mit Schließen der Streams funktioniert das nicht.
Die geänderten Daten bleiben gespeichert und die nicht geänderten werden gar nicht mehr in die datei
aufgenommen.
 
Java:
input = new FileInputStream(System.getProperty("user.home") + "/settings.cfg");
output = new FileOutputStream(System.getProperty("user.home") + "/settings.cfg");
Du hast die zwei Stream gleichzeitig auf der Datei.
Andere das mal ab und öffne den OutputStream erst unmittelbar bevor die Properties gespeichert werden:
Properties einlesen --> InputStream schliessen
OutputStream öffenen --> Properties speichern -->OutputStream schliessen.
 
Java:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
 
 
public class TestConfig {
    
    public FileInputStream input;
    public FileOutputStream output;
    public Properties property = new Properties();
    private Boolean filled_empty_file = null;
    
    public TestConfig() {
        File settings = new File(System.getProperty("user.home") + "/settings.cfg");
        if (settings.exists() == false) {
            try {
                settings.createNewFile();
                this.filled_empty_file = false;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            input = new FileInputStream(System.getProperty("user.home") + "/settings.cfg");
            output = new FileOutputStream(System.getProperty("user.home") + "/settings.cfg");
            property.load(input);
            if (this.filled_empty_file == false) {
                this.setValue("MyKey2", "MyValue2");
                this.setValue("MyKey3", "MyValue3");
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    }
    
    public String getValue(String key) {
        return property.getProperty(key);
    }
    
    public void setValue(String key, String value) {
        property.setProperty(key, value);
    }
    
    public void saveValue() {
        try {
            property.store(output, null);
            input.close();
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        TestConfig config = new TestConfig();
        config.setValue("MyKey", "MyValue");
        config.saveValue();
        System.out.println("MyKey: " + config.getValue("MyKey") + "\nMyKey2: " + config.getValue("MyKey2") + "\nMyKey3: " + config.getValue("MyKey3"));
    }
    
}

Dannach ist die settings-file leer.

\\Edit: Habs jetzt 🙂
 
Zuletzt bearbeitet:

Neue Themen


Zurück
Oben