读书人

带参数读书资源文件一

发布时间: 2012-10-13 11:38:17 作者: rapoo

带参数读书资源文件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
{

读书人网 >软件架构设计

热点推荐