- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Properties;
- /**
- * @author JAVA世纪网(java2000.net, laozizhu.com)
- */
- public class Test {
- public static void main(String[] args) {
- }
- }
- /**
- * @authorleno 单子模式,保证在整个应用期间只加载一次配置属性文件
- */
- class Singleton {
- private static Singleton instance;
- private static final String CONFIG_FILE_PATH = "E://config.properties";
- private Properties config;
- private Singleton() {
- config = new Properties();
- InputStream is;
- try {
- is = new FileInputStream(CONFIG_FILE_PATH);
- config.load(is);
- is.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static Singleton getInstance() {
- if (instance == null) {
- instance = new Singleton();
- }
- return instance;
- }
- public Properties getConfig() {
- return config;
- }
- public void setConfig(Properties config) {
- this.config = config;
- }
- }
单例模式取配置文件
最新推荐文章于 2023-07-08 13:40:10 发布