parent
6e55236a7d
commit
a5d860cb35
1 changed files with 50 additions and 0 deletions
50
src/main/java/eu/m724/autopeerer/common/Configuration.java
Normal file
50
src/main/java/eu/m724/autopeerer/common/Configuration.java
Normal file
|
@ -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)));
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue