diff --git a/src/main/java/eu/m724/autopeerer/common/Configuration.java b/src/main/java/eu/m724/autopeerer/common/Configuration.java new file mode 100644 index 0000000..cfde11d --- /dev/null +++ b/src/main/java/eu/m724/autopeerer/common/Configuration.java @@ -0,0 +1,50 @@ +package eu.m724.autopeerer.common; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.nio.file.Files; +import java.util.Properties; + +public class Configuration { + public final File directory = new File("config"); + private final File configFile; + + private final Properties properties = new Properties(); + + public Configuration(String profile) { + this.configFile = new File(directory, profile + ".properties"); + } + + + public void load() throws IOException { + directory.mkdir(); + + if (!configFile.exists()) { + try (var is = getClass().getClassLoader().getResourceAsStream(configFile.getName())) { + Files.write(configFile.toPath(), is.readAllBytes()); + properties.load(is); + } + } + + try (var is = new FileInputStream(configFile)) { + properties.load(is); + } + } + + public String getString(String property) { + return properties.getProperty(property); + } + + public String getString(String property, String def) { + return properties.getProperty(property, def); + } + + public int getInt(String property) { + return Integer.parseInt(getString(property)); + } + + public int getInt(String property, int def) { + return Integer.parseInt(getString(property, String.valueOf(def))); + } +}