ZSIK_Projekt/src/main/java/ftp/sar/PropertiesLoader.java

56 lines
1.4 KiB
Java
Raw Normal View History

2019-12-02 18:23:39 +01:00
package ftp.sar;
2019-11-23 22:02:12 +01:00
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
public class PropertiesLoader {
2019-11-24 11:50:30 +01:00
public static final String CONFIG_NAME_FILE = "config.properties";
2019-11-23 22:02:12 +01:00
private static final PropertiesLoader instance = new PropertiesLoader();
private ResourceLoader resourceLoader = ResourceLoader.getInstance();
2019-11-24 11:50:30 +01:00
private Properties properties = null;
2019-11-23 22:02:12 +01:00
public PropertiesLoader () {}
public static PropertiesLoader getInstance() {
return instance;
}
public Properties load() {
2019-11-24 11:50:30 +01:00
this.properties = new Properties();
2019-11-23 22:02:12 +01:00
try {
2019-11-24 11:50:30 +01:00
InputStream inputStream = resourceLoader.getResourceAsStream(CONFIG_NAME_FILE);
this.properties.load(inputStream);
2019-11-23 22:02:12 +01:00
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
2019-11-24 11:50:30 +01:00
return this.properties;
2019-11-23 22:02:12 +01:00
}
public void save(Properties prop) {
try {
2019-11-24 11:50:30 +01:00
OutputStream output = new FileOutputStream(this.resourceLoader.getPath(CONFIG_NAME_FILE));
2019-11-23 22:02:12 +01:00
prop.store(output, null);
output.close();
System.out.println(prop);
} catch (IOException e){
e.printStackTrace();
}
}
2019-11-24 11:50:30 +01:00
public String get(String name) {
if (this.properties == null) {
this.load();
}
return this.properties.getProperty(name);
}
2019-11-23 22:02:12 +01:00
}