Skip to content

Instantly share code, notes, and snippets.

@skrb
Last active August 29, 2015 14:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skrb/9202e62ade9d705e879b to your computer and use it in GitHub Desktop.
Save skrb/9202e62ade9d705e879b to your computer and use it in GitHub Desktop.
UTF-8のプロパティファイルを読むための snipet
ResourceBundle.Control control = new ResourceBundle.Control() {
@Override
public ResourceBundle newBundle(String baseName, Locale locale, String format,
ClassLoader loader, boolean reload)
throws IllegalAccessException, InstantiationException, IOException {
String bundleName = toBundleName(baseName, locale);
ResourceBundle bundle = null;
if (format.equals("java.properties")) {
final String resourceName = toResourceName(bundleName, "properties");
final ClassLoader classLoader = loader;
final boolean reloadFlag = reload;
InputStream stream = null;
if (reloadFlag) {
URL url = classLoader.getResource(resourceName);
if (url != null) {
URLConnection connection = url.openConnection();
if (connection != null) {
connection.setUseCaches(false);
stream = connection.getInputStream();
}
}
} else {
stream = classLoader.getResourceAsStream(resourceName);
}
if (stream != null) {
try {
bundle = new PropertyResourceBundle(new InputStreamReader(stream, "UTF-8"));
} finally {
stream.close();
}
}
} else {
throw new IllegalArgumentException("unknown format: " + format);
}
return bundle;
}
};
ResourceBundle resourceBundle = ResourceBundle.getBundle(LOCALE_BUNDLE_NAME,
resourceBundleLocale,
Localization.class.getClassLoader(),
control);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment