java处理国际化和读取properties文件代码片段
1、java处理国际化片段
static
{
logger.log(Level.INFO, "开始加载语言环境");
// 默许中文
locale = new Locale(UtilsConstants.UTILS_LANGUAGE,
UtilsConstants.UTILS_COUNTRY);
messages = ResourceBundle.getBundle(ConfigFilePath.getpackagesPath()
+ UtilsConstants.UTILS_BASE_RES, locale);
}
public String getMessage(String target)
{
try
{
// TODO Auto-generated method stub
return messages.getString(target);
} catch (MissingResourceException e)
{
logger.log(Level.SEVERE,
messages.getString("utils.message.log.NoKeyInFile")
+ "[ " + target + " ]");
return "";
}
}
2、java读写properties文件
/**
* 初始化加载
*/
static
{
logger.log(Level.INFO, getMessage("utils.message.log.startLoadFile"));
InputStream in = null;
// 构建属性文件路径
String filePath = ConfigFilePath.getFilePaths() + fileName;
// 加载属性文件
try
{
in = new BufferedInputStream(new FileInputStream(filePath));
properties.load(in);
logger.log(Level.INFO, getMessage("utils.message.log.endLoadFile"));
} catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
logger.log(Level.SEVERE,
getMessage("utils.message.log.createFileStreamFail"), e);
} catch (IOException e)
{
// TODO Auto-generated catch block
logger.log(Level.SEVERE,
getMessage("utils.message.log.loadPropertyFileFail"), e);
} finally
{
try
{
in.close();
} catch (IOException e)
{
// TODO Auto-generated catch block
logger.log(Level.SEVERE,
getMessage("utils.message.log.closeFileStreamFail"), e);
}
}
}
@Override
public String readProperty(String key)
{
// TODO Auto-generated method stub
if (null == properties.getProperty(key))
{
logger.log(Level.SEVERE,
getMessage("utils.message.log.propertyIsNull"));
}
return properties.getProperty(key);
}
@Override
public boolean writeProperty(String key, String value)
{
// TODO Auto-generated method stub
properties.setProperty(key, value);
String filePath = ConfigFilePath.getFilePaths() + fileName;
OutputStream out = null;
// 保存属性文件
try
{
out = new BufferedOutputStream(new FileOutputStream(filePath));
properties.store(out, "");
} catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
logger.log(Level.SEVERE,
getMessage("utils.message.log.createFileStreamFail"), e);
} catch (IOException e)
{
// TODO Auto-generated catch block
logger.log(Level.SEVERE,
getMessage("utils.message.log.stroePropertyFileFail"), e);
} finally
{
try
{
out.close();
} catch (IOException e)
{
// TODO Auto-generated catch block
logger.log(Level.SEVERE,
getMessage("utils.message.log.closeFileStreamFail"), e);
}
}
return false;
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。