Skip to content

Instantly share code, notes, and snippets.

@komiya-atsushi
Created September 14, 2014 18:04
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save komiya-atsushi/2d9d8b2000358d4d004d to your computer and use it in GitHub Desktop.
Save komiya-atsushi/2d9d8b2000358d4d004d to your computer and use it in GitHub Desktop.
UTF-8 エンコーディングされたプロパティファイルを ResourceBundle クラスで取り扱う。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
/**
* UTF-8 エンコーディングされたプロパティファイルを {@link ResourceBundle} クラスで取り扱う。
*/
public class ResourceBundleWithUtf8 {
private static ResourceBundle.Control UTF8_ENCODING_CONTROL = new ResourceBundle.Control() {
/**
* UTF-8 エンコーディングのプロパティファイルから ResourceBundle オブジェクトを生成します。
* <p>
* 参考 :
* <a href="http://jgloss.sourceforge.net/jgloss-core/jacoco/jgloss.util/UTF8ResourceBundleControl.java.html">
* http://jgloss.sourceforge.net/jgloss-core/jacoco/jgloss.util/UTF8ResourceBundleControl.java.html
* </a>
* </p>
*
* @throws IllegalAccessException
* @throws InstantiationException
* @throws IOException
*/
@Override
public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload)
throws IllegalAccessException, InstantiationException, IOException {
String bundleName = toBundleName(baseName, locale);
String resourceName = toResourceName(bundleName, "properties");
try (InputStream is = loader.getResourceAsStream(resourceName);
InputStreamReader isr = new InputStreamReader(is, "UTF-8");
BufferedReader reader = new BufferedReader(isr)) {
return new PropertyResourceBundle(reader);
}
}
};
public static void main(String[] args) {
ResourceBundle bundle = ResourceBundle.getBundle("utf8", UTF8_ENCODING_CONTROL);
System.out.println(bundle.getString("いろはにほへと"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment