使用单例读取配置文件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/s524061267/article/details/60757511

本文旨在整理个人工作总结,仅供参考


直接贴代码,java代码如下:

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @ClassName: WordPropertyUtil
 * @Description: 读取配置文件
 * @author sly.shuai
 * @date2017年2月22日 上午10:20:20
 * @version V1.0
 */
@SuppressWarnings("serial")
public class WordPropertyUtil extends Properties
{
    private static WordPropertyUtil instance;

    // 读取配置文件
    private WordPropertyUtil() {
        InputStream iss = null;
        try {
            iss = this.getClass().getResourceAsStream("/wordTemplate.properties");
            this.load(iss);
        } catch (IOException e) {
        } finally {
            if (iss != null) {
                try {
                    iss.close();
                } catch (IOException e) {
                }
            }
        }
    }

    // 单例模式
    public static WordPropertyUtil getInstance() {
        if (instance != null) {
            return instance;
        } else {
            instance = new WordPropertyUtil();
            return instance;
        }
    }
}

配置文件wordTemplate.properties内容如下:

#word导出模板参数配置

#银行签收单模板存储位置
templatePath = /sly_files_server/word/templatePath/

#银行签收单模板名字
templateName = test.ftl

#生成word的存储位置
filePath = /sly_files_server/word/filePath/

#生成word的名字
fileName = test.doc

具体调用方式如下:

String templatePath = WordPropertyUtil.getInstance().getProperty("templatePath");
String templateName = WordPropertyUtil.getInstance().getProperty("templateName");
String filePath = WordPropertyUtil.getInstance().getProperty("filePath");
String fileName = WordPropertyUtil.getInstance().getProperty("fileName");

猜你喜欢

转载自blog.csdn.net/s524061267/article/details/60757511