Beispiele für Java Properties-Dateien
Normalerweise wird die Java-Eigenschaftendatei zum Speichern von Projektkonfigurationsdaten oder -einstellungen verwendet. In diesem Tutorial zeigen wir Ihnen, wie Sie in eine.properties-Datei lesen und daraus schreiben.
Properties prop = new Properties();
// set key and value
prop.setProperty("db.url", "localhost");
prop.setProperty("db.user", "example");
prop.setProperty("db.password", "password");
// save a properties file
prop.store(outputStream, "");
// load a properties file
prop.load(inputStream)
// get value by key
prop.getProperty("db.url");
prop.getProperty("db.user");
prop.getProperty("db.password");
// get all keys
prop.keySet();
// print everything
prop.forEach((k, v) -> System.out.println("Key : " + k + ", Value : " + v));
Eine einfache Maven-Projektstruktur zum Testen.

1. Schreiben Sie in die Eigenschaftendatei
Legen Sie den Eigenschaftsschlüssel und den Wert fest und speichern Sie ihn irgendwo.
App1.java
package com.example;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;
public class App1 {
public static void main(String[] args) {
try (OutputStream output = new FileOutputStream("path/to/config.properties")) {
Properties prop = new Properties();
// set the properties value
prop.setProperty("db.url", "localhost");
prop.setProperty("db.user", "example");
prop.setProperty("db.password", "password");
// save properties to project root folder
prop.store(output, null);
System.out.println(prop);
} catch (IOException io) {
io.printStackTrace();
}
}
}
Ausgabe
{db.user=example, db.password=password, db.url=localhost}
Daspath/to/config.properties wird erstellt.
path/to/config.properties
#Thu Apr 11 17:37:58 SRET 2019 db.user=example db.password=password db.url=localhost
2. Laden Sie eine Eigenschaftendatei
Laden Sie eine Eigenschaftendatei aus dem Dateisystem und rufen Sie den Eigenschaftswert ab.
App2.java
package com.example;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class App2 {
public static void main(String[] args) {
try (InputStream input = new FileInputStream("path/to/config.properties")) {
Properties prop = new Properties();
// load a properties file
prop.load(input);
// get the property value and print it out
System.out.println(prop.getProperty("db.url"));
System.out.println(prop.getProperty("db.user"));
System.out.println(prop.getProperty("db.password"));
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
Ausgabe
localhost example password
3. Laden Sie eine Eigenschaftendatei aus dem Klassenpfad
Laden Sie eine Eigenschaftendateiconfig.properties aus dem Projektklassenpfad und rufen Sie den Eigenschaftswert ab.
src/main/resources/config.properties
db.url=localhost db.user=example db.password=password
App3.java
package com.example;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class App3 {
public static void main(String[] args) {
try (InputStream input = App3.class.getClassLoader().getResourceAsStream("config.properties")) {
Properties prop = new Properties();
if (input == null) {
System.out.println("Sorry, unable to find config.properties");
return;
}
//load a properties file from class path, inside static method
prop.load(input);
//get the property value and print it out
System.out.println(prop.getProperty("db.url"));
System.out.println(prop.getProperty("db.user"));
System.out.println(prop.getProperty("db.password"));
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
Ausgabe
localhost example password
4. Druckt alles aus einer Eigenschaftendatei
Laden Sie eine Eigenschaftendateiconfig.properties aus dem Projektklassenpfad und drucken Sie die Schlüssel und Werte aus.
App4.java
package com.example;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.Set;
public class App4 {
public static void main(String[] args) {
App4 app = new App4();
app.printAll("config.properties");
}
private void printAll(String filename) {
try (InputStream input = getClass().getClassLoader().getResourceAsStream(filename)) {
Properties prop = new Properties();
if (input == null) {
System.out.println("Sorry, unable to find " + filename);
return;
}
prop.load(input);
// Java 8 , print key and values
prop.forEach((key, value) -> System.out.println("Key : " + key + ", Value : " + value));
// Get all keys
prop.keySet().forEach(x -> System.out.println(x));
Set
Ausgabe
Key : db.user, Value : example Key : db.password, Value : password Key : db.url, Value : localhost db.user db.password db.url
Quellcode herunterladen
Herunterladen -java-properties-file.zip (6 KB)