使用单例读取配置文件

来源:互联网 发布:淘宝diy电脑哪家好 编辑:程序博客网 时间:2024/05/20 00:17

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


直接贴代码,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");
1 0
原创粉丝点击