首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

带参数读书资源文件一

2012-10-13 
带参数读书资源文件1MessageUtil.getMessage(key, request.getLocale(), string1, string2.......)impo

带参数读书资源文件1
MessageUtil.getMessage("key", request.getLocale(), string1, string2.......);

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;


public class MessageUtil
{
   
    // 当前应用的路径
    private static String contextPath = PathCommon.getWebRoot();
   
    // 资源路径
    private static final String messagePath = contextPath + File.separator
            + "WEB-INF" + File.separator + "classes";
   
    private Properties messages;
   
    private static final Map<String, Properties> messageMap = new HashMap<String, Properties>();
   

    private Locale locale;
   
    public MessageUtil(String path, String fileName, Locale locale)
    {
        try
        {
            this.locale = locale;
            this.messages = getPropertiesFromFile(path, fileName, locale);
        }
        catch (Exception e)
        {
            log.error(e);
        }
    }
   

     * 读出配置文件中的信息
     *
/
    public static Properties getPropertiesFromFile(String filePath,
            String fileName, Locale locale)
    {
        Properties messages = new Properties();
       
        //检查参数合法性
        if (StringUtils.isEmpty(filePath))
        {
            throw new IllegalArgumentException(
                    "File path cannot be empty or null!");
        }
       
        if (StringUtils.isEmpty(fileName))
        {
            throw new IllegalArgumentException(
                    "File name cannot be empty or null!");
        }
       
        final int dotIndex = fileName.indexOf(".");
       
        //文件名不包含.则抛出异常
        if (dotIndex < 0)
        {
        }
        final String prefix = fileName.substring(0, dotIndex);
        final String suffix = fileName.substring(dotIndex);
        FileInputStream fis = null;
        try
        {

热点排行