Hi,
ich habe mir eine config.property Datei erstellt und dort "path = " festgelegt ohne die Hochkommata.
Dieses liegt im Root-Verzeichnis meines Java-Projekts und das Schreiben mit dieser Methode funktioniert, allerdings nicht das Lesen dieser config.property.
Als Fehlermeldung erhalte ich eine Nullpointerexception.
Code fürs Schreiben:
Code fürs Lesen:
Beim Wert von configFile habe ich auch schon \\ und ohne Slashs probiert.
Ich hatte es mal hinbekommen, dass ich mir die Property-Datei aus dem Resource Ordner holen kann und auslesen kann, allerdings hat man keine Schreibrechte auf den Resource-Ordner (habe ich gelesen), weswegen ich die Datei gerne einfach im Projektverzeichnis hätte.
Also der Nullpointer tritt bei inputStream.close im Finally-Statement auf, da inputstream einfach null bleibt.
Wo liegt hier mein Problem?
ich habe mir eine config.property Datei erstellt und dort "path = " festgelegt ohne die Hochkommata.
Dieses liegt im Root-Verzeichnis meines Java-Projekts und das Schreiben mit dieser Methode funktioniert, allerdings nicht das Lesen dieser config.property.
Als Fehlermeldung erhalte ich eine Nullpointerexception.
Code fürs Schreiben:
Code:
public void writeToProperty(String path) {
try {
output = new FileOutputStream("config.properties");
// set the properties value
prop.setProperty("path", path);
// save properties to project root folder
prop.store(output, null);
} catch (IOException io) {
io.printStackTrace();
} finally {
if (output != null) {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Code fürs Lesen:
Code:
public String getPropValues() throws IOException {
try {
String configFile = "config.properties";
inputStream = this.getClass().getClassLoader().getResourceAsStream(configFile);
if (inputStream != null) {
prop.load(inputStream);
} else {
throw new FileNotFoundException("property file '" + configFile + "' not found in the classpath");
}
Date time = new Date(System.currentTimeMillis());
// get the property value and print it out
String path = prop.getProperty("path");
result = path;
System.out.println("The config.properties file has following content\npath: " + result + "\time: " + time);
} catch (Exception e) {
System.out.println("Exception: " + e);
} finally {
inputStream.close();
}
return result;
}
Beim Wert von configFile habe ich auch schon \\ und ohne Slashs probiert.
Ich hatte es mal hinbekommen, dass ich mir die Property-Datei aus dem Resource Ordner holen kann und auslesen kann, allerdings hat man keine Schreibrechte auf den Resource-Ordner (habe ich gelesen), weswegen ich die Datei gerne einfach im Projektverzeichnis hätte.
Also der Nullpointer tritt bei inputStream.close im Finally-Statement auf, da inputstream einfach null bleibt.
Wo liegt hier mein Problem?