JAva処理国際化とpropertiesファイルコードクリップの読み取り
4207 ワード
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;
}