当前位置: 首页 > 知识库问答 >
问题:

用ini4j实现单例模式

曹光霁
2023-03-14

原始问题:程序读取旧配置文件(.ini)的输入

import org.ini4j.Wini;
import java.io.*;

//http://www.javenue.info/post/40

public class Configuration {
    private static Configuration _instance = null;

    private Wini ini = null;
    FileInputStream stream;

    private Configuration() {
        ini= new Wini();
        try {
            stream = new FileInputStream(Constants.PATH);
            ini.load(stream);
        }
        catch (Exception e) {
            System.out.println("FILE NOT FOUND!");
        }
    }

    public synchronized static Configuration getInstance() {
        if (_instance == null)
            _instance = new Configuration();
        return _instance;
    }

    public String getConfig(String xSectionName, String xFieldValue){

        String readValue = null;

        if (ini.get(xSectionName, xFieldValue) != null) {
            readValue = ini.get(xSectionName, xFieldValue);
        } else {
            // TODO: What should happen
        }
        return readValue;
    }

    public void setConfig(String xSectionName, String xFieldValue, String xValue){

        System.out.println("Section: " + xSectionName);
        System.out.println("Field:   " + xFieldValue);
        System.out.println("Value:   " + xValue + "\n\n");

        try {
            ini.put(xSectionName, xFieldValue, xValue);
            ini.store();
        } catch (Exception e1) {
            System.out.println(xValue + " could not be stored.");
            e1.printStackTrace();
        }
    }
}

小节:漂移

田块:亩

价值:5

import net.openhft.compiler.CompilerUtils;

...

ClassLoader classloader = new ClassLoader() {
        };

Class aClass = CompilerUtils.CACHED_COMPILER.loadFromJava(classloader, className, javaCode);

Callable<Object[]> caller = (Callable<Object[]>) aClass.newInstance();

Object[] obj = (Object[]) caller.call();

...

共有1个答案

卫君博
2023-03-14

我刚刚查看了源代码。
问题是,如果您希望使用相同的wini实例来调用store()方法,则应该避免在wini类中加载void load(InputStream input)来加载ini文件。
为什么?因为store()方法希望在当前wini的实例中有一个现有的文件字段,但它找不到它,因为您是从流而不是文件加载ini的。

因此,应该使用wini构造函数加载ini文件。构造函数将文件作为参数,它还在实例中设置字段以加载ini内容。

看看这个:

public Wini(File input) throws IOException, InvalidFileFormatException{
        this();
        setFile(input);
        load();
}
 private Configuration() {
        ini = new Wini();
        try {
            stream = new FileInputStream(Constants.PATH);
            ini.load(stream);
        }
        catch (Exception e) {
            System.out.println("FILE NOT FOUND!");
        }
    }

由此:

private Configuration()  {
    try {
        ini= new Wini(new File(Constants.PATH));
    }
    catch (Exception e) {
        LOGGER.error("Exception during init of Configuration",e);
    }
}

下面是使用logging to log exception的完整类,它使用惰性单例实现,而不使用任何显式同步作为synchronized:

public class Configuration {

    private static Logger LOGGER = Logger.getLogger(Configuration.class)//

    private static class HolderLazySingleton {
        private static Configuration instance = new Configuration();
    }

    private Wini ini = null;

    private Configuration() {    
        try {
            ini = new Wini(new File(Constants.PATH));    
        } catch (Exception e) {
            LOGGER.error("Exception during init of Configuration",e);
        }
    }

    public static Configuration getInstance() {
        return HolderLazySingleton.instance;
    }

    public String getConfig(String xSectionName, String xFieldValue) {

        String readValue = null;

        if (ini.get(xSectionName, xFieldValue) != null) {
            readValue = ini.get(xSectionName, xFieldValue);
        } else {
            // TODO: What should happen
        }
        return readValue;
    }

    public void setConfig(String xSectionName, String xFieldValue, String xValue) {

        System.out.println("Section: " + xSectionName);
        System.out.println("Field:   " + xFieldValue);
        System.out.println("Value:   " + xValue + "\n\n");

        try {
            ini.put(xSectionName, xFieldValue, xValue);
            ini.store();
        } catch (Exception e1) {
            LOGGER.error(xValue + " could not be stored.", e1);         
        }
    }
}
 类似资料:
  • 本文向大家介绍JavaScript实现单例模式实例分享,包括了JavaScript实现单例模式实例分享的使用技巧和注意事项,需要的朋友参考一下 传统单例模式 保证一个类仅有一个实例,并提供一个访问它的全局访问点。 实现单例核心思想 无非是用一个变量来标志当前是否已经为某个类创建过对象,如果是,则在下一次获取该类的实例时,直接返回之前创建的对象,接下来我们用JavaScript来强行实现这个思路,请

  • 本文向大家介绍c# 单例模式的实现,包括了c# 单例模式的实现的使用技巧和注意事项,需要的朋友参考一下   记一下学习单例模式的笔记:   单例就是要保证该类仅有一个实例。实现完全封闭的单例(外部不能new)其实就要两点要求: 全局访问:需要一个该类型的全局静态变量,每次获取实例时都要判断它是否null,不存在new,存在通过一个方法直接返回该值获取实例来保证对象唯一; 实例化控制:new实例不能

  • 问题内容: 谁能提供一个单例模式的示例并解释为什么有必要吗? 问题答案: 在走单身路线之前,请重新考虑。您真的需要单身人士吗?如果您需要实现单例的方案,那是因为对它们的需求并没有真正表达出来。您最好不要仅在代码库中引入单例,因为遵循设计模式会很酷。 干净的代码讨论-全局状态和单例 一次还不够 表演者单身人士 但是,真正值得了解的是Dependency Injection 。 现在,如果您真的想用J

  • [ini4j] 是一个简单的 Java 类库,用来读写 Windows 的 ini 配置文件。同时还包含一个 Java Perferences API 的实现。 Maven: <dependency>    <groupId>org.ini4j<groupId>    <artifactId>ini4j</artifactID>    <version>X.Y.Z</version></depen

  • 本文向大家介绍js实现简单模态框实例,包括了js实现简单模态框实例的使用技巧和注意事项,需要的朋友参考一下 模态框在网页中很常见就是在当前页面中弹出一个框供与客户交互。 类似于这样。 首先我们要明白该框工作原理至于怎样与后端进行交互联系这边先不做介绍我们首先是单纯的了解怎样在网页中实现这样的一个框图的显现。值得注意的是框图产生时一般的我们滚动鼠标发现网页仍在移动。实现这样框图就是加了两个盒子 第一

  • 本文向大家介绍php单例模式的简单实现方法,包括了php单例模式的简单实现方法的使用技巧和注意事项,需要的朋友参考一下 php单例模式的简单实现方法 首先我们要知道明确单例模式这个概念,那么什么是单例模式呢? 单例模式顾名思义,就是只有一个实例。 作为对象的创建模式, 单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例, 这个类我们称之为单例类。 单例模式的要点有三个: 一是