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;
}