Last active
August 29, 2015 14:02
-
-
Save skrb/9202e62ade9d705e879b to your computer and use it in GitHub Desktop.
UTF-8のプロパティファイルを読むための snipet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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